STRING IN JAVA
STRING
In Java string is basically an object that represents sequence of char values. An arrays of characters works same as Java string. For example.
Char [] ch = {"a","v"};
String srr = new string (ch);
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
There are two ways to create String object:
- By string literal
- By new keyword
This is what happens when you create string objects using string literal,
“When you create a string object using string literal, JVM first checks the content of to be created object. If there exist an object in the pool with the same content, then it returns the reference of that object. It doesn’t create new object. If the content is different from the existing objects then only it creates new object.”
One more interesting thing about String objects in java is that they are immutable. That means once you create a string object, you cannot modify the contents of that object. If you try to modify the contents of string object, a new string object is created with modified content
string vs string buffer vs string builder
String -
1) string is immutable.
2) string is thread-safe.
StringBuffer -
1) StringBuffer is mutable means it can be change.
2) it is thread-safe
3) it is slow in performance as compared to Stringbuilder

Comments
Post a Comment