Saturday, May 30, 2015

StringBuilder/Tokenizer

import java.util.StringTokenizer;

//  STRINGS and STRING BUILDER and STRING BUFFER

public class ClassA {

public static void main(String[] args) {

System.out.println("-----STRING CASE: This create two object-----");
String str = new String("Ram");
str.concat("Hari");
  // Here if we don't assign the value to str we get the output Ram only

str = str.concat("Hari");
System.out.println(str);

System.out.println("\nSTRING BUILDER CASE:  -----This create only one object-----");

StringBuilder strBui = new StringBuilder("Ram");
strBui.append("Hari");
   // Here we don't have to assign the value to strBui cause it directly appends to the same object

System.out.println(strBui);

System.out.println("\nStringBuilder and StringBuffer do the same job but different is that                StringBuffer is synchronized which is thread safe");

System.out.println("\nSTRING TOKENIZER CASE:  -----This create only one object-----");
StringTokenizer st = new StringTokenizer("This is Java class");

//StringTokenizer st = new StringTokenizer("This is Java class","i");
  //Break the value from "i"

while(st.hasMoreElements()){
System.out.println(st.nextElement());
}
}
}


No comments:

Post a Comment