The difference among String, StringBuffer and StringBuilder-
String is immutable final class i.e. once it created and initialize, can not be modified. It is found in java.lang package. The some reason to make it final immutable class are-
String is created by below two ways-
Answer is: only Object storage location will be different in memory. e.g. if we use statement String str = "abc" then Object will be stored in constant pool and if we use Statement String str= new String("abc") then Object will be stored in non-constant pool.Now the question is for below scenario, how many object will be created in constant pool?
Now what about below scenario, how many object will be created in non constant pool?
Now as you know both ways of String creation so question is-
String class has various method like-
are mutable so they can change their values. And always created by new operator like-
If your string is not going to change use a String class because a String object is immutable.
If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.
- Caching- Immutable String Object will hold same hashcode throughout life so, it works correctly in HashMap or Hashtable.
- Data security- Classloader loads class by class name and class name format is String so this name can not be change dynamically hence always load without any fail.
- Thread safeness- Immutable objects are safe when shared between multiple threads in multi-threaded applications.
String is created by below two ways-
- String str ="abc";
- String str = new String("abc");
Answer is: only Object storage location will be different in memory. e.g. if we use statement String str = "abc" then Object will be stored in constant pool and if we use Statement String str= new String("abc") then Object will be stored in non-constant pool.Now the question is for below scenario, how many object will be created in constant pool?
String str ="abc"; String str1="abc"; String str2=str1;Answer is only one object. The reason behind this is that if same String Object is exist in constant pool then it will not create another String in constant pool, and same String is referenced by str1 and similarly str2. So, only one object will be created in constant pool.
Now what about below scenario, how many object will be created in non constant pool?
String str =new String("abc"); String str1=new String("abc"); String str2=str1;Answer is two object. The reason behind this is that if Object is created by new operator then every time new object will be created in non constant pool whether object is same or different no matter here and str2 will point to second object. So,two object will be created in non constant pool in this case.
Now as you know both ways of String creation so question is-
String str= "abc"; String str1= new String("abc"); if(str==str1){ System.out.println("true"); }else{ System.out.println("false"); }What will be the answer of above- run it and you will find that answer will be false. because when we compare two String literals using equality operator "==" it returns true if they are referring same memory location or instance of String. in this case one is in constant pool and other is in non constant pool so object location in memory is not same hence false will be returned.
String class has various method like-
- boolean equals(Object obj)- Compare the String with object.
- String concat(String str)- Concat the str to current string.
- int compareTo(String str)- Compares two strings lexicographically
- int charAt(int index)- return the specified character of String
- int hashCode()- return hashCode representation of String
- int indexOf(int ch) and int lastIndexOf(int ch)- Returns the index within this string of the first occurrence of the specified char and in second method Returns the index within this string of the last occurrence of the specified char
StringBuffer sbr= new StringBuffer(); StringBuilder sbr= new StringBuilder();The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer. So we can say-
If your string is not going to change use a String class because a String object is immutable.
If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.
No comments:
Post a Comment