Ticker

6/recent/ticker-posts

String class - String , StringBuffer and StringBuilder With Example

String class 

Java String is a immutable object. For an immutable object you cannot modify any of its attribute’s values. Once you have created a java String object it cannot be modified to some other object or a different String. String is one of the widely used java classes 

  • Strings are not null terminated in Java. 
  • Strings are immutable and final in Java 
  • Strings are maintained in String Pool 
  • Use equals methods for comparing String in Java 
  • Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String 
  • Use SubString to get part of String in Java 
  • "+" is overloaded for String concatenation 
  • Use trim() to remove white spaces from String 
  • Use split() for splitting String using Regular expression 

Difference between String , StringBuffer and StringBuilder 

The most important difference between String and StringBuffer , StringBuilder in java is that String objects are immutable ( can’t change the content of String Object) 

StringBuffer and StringBuilder objects are mutable ( can change the content of StringBuffer and StringBuilder objects). StringBuilder and StringBuffer are almost the same. 

The difference is that StringBuffer is synchronized and StringBuilder is not. Although, StringBuilder is faster than StringBuffer, the difference in performance is very little. StringBuilder is a SUN's replacement of StringBuffer. It just avoids synchronization from all the public methods. Rather than that, their functionality is the same. 

We can create the objects of String , StringBuilder and StringBuffer has shown below. There are few methods in each class refer them. 

String 

String name = “vikas”; 

String place = new String(“banavara”); 

String buffer 

StringBuffer sbf = new StringBuffer( ); 

sbf.append(“Hassan”); 

StringBuffer sbf = new StringBuffer(10); 

sbf.append(“Banglore”); 

StringBuffer sbf = new StringBuffer(“Mysore”); 

String builder 

StringBuilder sbr = new StringBuilder ( ); 

sbr.append(“Kannada”); 

StringBuilder sbr = new StringBuilder (10); 

sbr.append(“Kaaveri”); 

StringBuilder sbr = new StringBuilder (“Mysore”); 

ALSO SEARCH:

"string and stringbuilder in java"

"stringbuffer in java"

"when to use stringbuffer and stringbuilder"

"stringbuilder java"

"difference between string and stringbuffer and stringbuilder in java"

"difference between string and stringbuffer and stringbuilder in java in tabular form"