Java string , string buffer and wrapper class

1,165 views 10 slides Aug 23, 2018
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Java string , string buffer and wrapper class


Slide Content

Java String  class provides a lot of methods to perform operations on string such as compare(), concat (), equals(), split(), length(), replace(), compareTo (), intern(), substring() etc. String The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in java by using these three classes. public   class   StringExample {   public   static   void  main(String  args []){   String s1="java";//creating string by java string literal   char   ch []={' s','t','r','i','n','g','s '};   String s2= new  String( ch );//converting char array to string   String s3= new  String("example");//creating java string by new keyword   System.out.println (s1);   System.out.println (s2);   System.out.println (s3);   }}   Output : java strings example

Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed . Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order. Java StringBuffer class Constructor Description StringBuffer() creates an empty string buffer with the initial capacity of 16. StringBuffer(String str) creates a string buffer with the specified string. StringBuffer ( int capacity) creates an empty string buffer with the specified capacity as length. Important Constructors of StringBuffer class

StringBuffer examples Some of the methods of stringbuffer class is as follows: 1. StringBuffer append() method The append() method concatenates the given argument with this string. class   StringBufferExample {   public   static   void  main(String  args []){   StringBuffer   sb = new   StringBuffer ("Hello ");   sb.append ("Java");//now original string is changed   System.out.println ( sb );//prints Hello Java   }   }  

2 ) StringBuffer insert() method The insert() method inserts the given string with this string at the given position. class  StringBufferExample2{   public   static   void  main(String  args []){   StringBuffer   sb = new   StringBuffer ("Hello ");   sb.insert (1,"Java");//now original string is changed   System.out.println ( sb );//prints  HJavaello    }   }   3 ) StringBuffer replace() method The replace() method replaces the given string from the specified beginIndex and endIndex . class  StringBufferExample3{   public   static   void  main(String  args []){   StringBuffer   sb = new   StringBuffer ("Hello");   sb.replace (1,3,"Java");   System.out.println ( sb );//prints  HJavalo    }   }   Conti.

4) StringBuffer delete() method The delete() method deletes the string from the specified beginIndex to endIndex . class  StringBufferExample4{   public   static   void  main(String  args []){   StringBuffer   sb = new   StringBuffer ("Hello");   sb.delete (1,3);   System.out.println ( sb );//prints  Hlo    }   }   5 ) StringBuffer reverse() method The reverse() method of StringBuffer class reverses the current string. class  StringBufferExample5{   public   static   void  main(String  args []){   StringBuffer   sb = new   StringBuffer ("Hello");   sb.reverse ();   System.out.println ( sb );//prints  olleH    }   }  

Wrapper class in Java Wrapper class in java  provides the mechanism to convert primitive into object and object into primitive . The eight classes of  java.lang  package are known as wrapper classes in java. The list of eight wrapper classes are given below: Primitive type Wrapper class boolean Boolean int Integer byte Byte short Short char Charcter long Long float Float double Double

Wrapper class Example: Primitive to Wrapper public   class  WrapperExample1{   public   static   void  main(String  args []){   //convert primitive into object type   int  a=20;   Integer i= Integer.valueOf (a); //converting  int  into Integer  System.out.println (“printing as primitive type "+a);     System.out.println (“printing as object: ” + i)    }}   Output: 20 20

Wrapper class Example: Primitive to Wrapper public   class  WrapperExample2{     public   static   void  main(String  args []){     //Converting Integer to  int      Integer a= new  Integer(3);     int  i= a.intValue (); //converting Integer to  int          System.out.println (“printing as object  "+a);     System.out.println (“printing as primitive type ” + i)   }}     Output: 3 3 3

Thank you
Tags