A brief discussion about most common object of Java: String, StringBuffer and StringBuilder


String, StringBuffer, StringBuilder are belongs to java core library : java.lang. All these three are implements of CharSequenceInterface. When creating a String object, it creating a string that can not be changed. So when its changing a new string object is creating. This is actually called string immutable(unchanging over time or unable to be changed).

String’s or object’s toString() is when it used in concatenation expression or used in println() function.

The equals() method compares the characters inside a string object.
The == operator compares two object references whether they refer to the same instance.

StringBuilder is faster then StringBuffer but it is not synchronized or thread-safe.

Java hides most of the complexity of conversion between Strings and StringBuffers.
When the + operator is used on String object, then append method of StringBuffer called and internally the convesions are made.
There are many optimization that the Java run time can make knowing that String objects are immutable.
Because of performance, the String object is used most of the case.

Here are some the code examples:

1st example: String

package com.ivan.string; 
public class MakeString { 
 public static void main(String args[]){ 
   char c[] = {'J','a','v','a'}; 
   String s1 = new String(c); 
   String s2 = new String(s1); 
//   System.out.println(s1); 
//   System.out.println(s2); 
 //how to use asciiChars array bytes in string constructors 
   byte ascii[] = {65, 66, 67, 68, 69, 70}; 
   s1 = new String(ascii); 
   s2 = new String(ascii, 2, 3); 
//   System.out.println(s1); 
//   System.out.println(s2); 
   //how to use string buffer in string constructor 
   StringBuffer strBufObj = new StringBuffer("Hello"); 
   s1 = new String(strBufObj); 
   System.out.println("String value = " + s1); 
   System.out.println("String length = " + s1.length()); 
   System.out.println("StringBuffer length = " + strBufObj.length()); 
   System.out.println("StringBuffer capacity = "+ strBufObj.capacity()); 
   //example of charAt(), getChars(), getBytes, toCharArray() 
   //string comparison = equals(), equalslgonerCase(), regionMatches(), 
startsWth(), endsWith() 
   //equals() vs == 
   //searching strings = indexOf(), lastIndesOf() 
   //modifying string = substring(), concat(), replace(), trim() 
   //data conversion using valueOf() 
   //change case = toLowerCase(), toUpperCase() 

2nd example: StringBuffer

package com.ivan.string; 
public class MyStringBuffer { 
   public static void main(String args[]){ 
    //StringBuffer using of different constructor 
     StringBuffer strBufBlackObj = new StringBuffer(); 
     StringBuffer strBufFixedObj = new StringBuffer(20); 
     StringBuffer strBufValueObj = new StringBuffer("This is a string buffer"); 
     CharSequence chars = "This is a char sequence"; 
     StringBuffer strBufCharSeqObj = new StringBuffer(chars); 
	//StringBuffer length(), capacity(), ensureCapacity(), setLength(), charAt(), setCharAt(),  
	//System.out.println("strBufBlackObj length = " +  strBufBlackObj.length()); 
	//System.out.println("strBufFixedObj capacity = " + strBufFixedObj.capacity()); 
    int capacity = 999; 
	//System.out.println("strBufBlackObj ensureCapacity = " + strBufBlackObj.capacity()); 
    strBufBlackObj = strBufBlackObj.append("this is test"); 
	//if(strBufBlackObj.length() == strBufBlackObj.capacity()) 
		//System.out.println("strBufBlackObj new length = " + strBufBlackObj.length() + " strBufBlackObj new capacity = " + strBufBlackObj.capacity()); 
	//System.out.println("char of 0 index = " + strBufValueObj.charAt(0)); 
    strBufValueObj.setCharAt(0, 'I'); 
	strBufValueObj.setCharAt(1, 'V');    
	strBufValueObj.setCharAt(2, 'A');   
	strBufValueObj.setCharAt(3, 'N'); 
	//System.out.println("new char of 0 index = " + strBufValueObj.charAt(0)); 
	//getChars(), append(), insert(), reverse(), 
	//to copy a substring of a string buffer into an array => getChars() 
    char[] Str2 = new char[7]; 
          strBufValueObj.getChars(0, 5, Str2, 0); 
          System.out.print("Copied Value = " ); 
      }catch( Exception ex){ 
           System.out.println("Null pointer Raised exception..."); 
	//append(String Str), append(int num), append(Object obj) 
    int a = 42; 
    String test = " the end string"; 
    strBufValueObj.append(", int a = ").append(a).append(test); 
	//insert(int index, String Str), insert(int index, char ch), insert(int index,  Object obj) 
    strBufValueObj.insert(5, "like java "); 
	//System.out.println("strBufValueObj reverse = " + strBufValueObj.reverse()); 
	//delete(), deleteCharAt(), replace(), subString(), 
    //delete(int startIndex,int endIndex),  
	//deleteChar(int loc) 
    strBufValueObj.delete(15, strBufValueObj.length()); 
	//replace(int startIndex, int endIndex, String str) 
    strBufValueObj.replace(0, 3, "Iaan my son also "); 
	//substring(int startIndex) 
	//substring(int startIndex, int endIndex) 

One thought on “A brief discussion about most common object of Java: String, StringBuffer and StringBuilder

  1. String array:
    String[] toppings = new String[20];

    String[] toppings;
    toppings = new String[20];
    String[] toppings = {“Cheese”, “Pepperoni”, “Black Olives”};
    for (String s: toppings)
    Concatenating Strings:

    String to CharArray convert:
    String palindrome = “Dot saw I was Tod”;
    int len = palindrome.length();
    char[] tempCharArray = new char[len];
    palindrome.getChars(0, len, tempCharArray, 0);

    String Format:
    System.out.printf(“The value of the float ” +
    “variable is %f, while ” +
    “the value of the ” +
    “integer variable is %d, ” +
    “and the string is %s”,
    floatVar, intVar, stringVar);

    String fs;
    fs = String.format(“The value of the float ” +
    “variable is %f, while ” +
    “the value of the ” +
    “integer variable is %d, ” +
    ” and the string is %s”,
    floatVar, intVar, stringVar);

    Other Example:
    ” HEL L O “.replace(” “, “”)))
    System.out.println(“Hello “.concat(“World”));

Leave a Reply

Your email address will not be published. Required fields are marked *