You are on page 1of 7

String Handling

What is a string?  object of java.lang.String class


 A string is a sequence of characters placed in double quotes("").

 Zero or multiple characters placed inside “” is called a string literal.


 In Java, a string literal is an object which is an instance of java.lang.String.
 It means, any data placed inside "", will be considered as java.lang.String
class object.
Note: We can create the String object either by using the ‘new’ keyword or using the
double quotes(“”);. By seeing double quotes, the compiler and JVM recognize it as a
String object.

What is string Handling?


Performing different operations on string data is called string handling.
Ex:

Note: This chapter is rightly called as String Handling, not String Manipulation. Because,
using the classes String, StringBuffer and StringBuilder, we can perform not only String
manipulation operations but also String comparison and so on.

Different ways to store string data inside a program in JVM:


There are 4 ways.
1. char[] object
2. String class object
3. StringBuffer class object
4. StringBuilder class object
The actual way of storing string data is a char[] object.
Why String class is given when a char array is available to represent a sequence of
characters?
a. Typing a char array is difficult.

b. representing data using char array hampers readability.


c. No inbuilt methods to perform operations on data. But String, StringBuffer and
StringBuilder classes have inbuilt methods to perform operations on data.

d. Size limitation.
String class is given to store characters dynamically without size limitation
Explanation:
The limitation on arrays is size. Once the array is created with some size then its size
cannot be modified. So, if we use a character array to represent/store a sequence of
characters then we can only store characters up to their size.
If we want to store new characters beyond their size, we must create an array with the
new required size and should copy all old array characters to a new array and then we
have to store new values at the end. We should repeat the same process every time the
array is filled and want to add new characters.
Since this operation must be done by every developer, to avoid this repeated operation,
SUN has given the String class in java.lang package.
Hence, using the String class we can store a sequence of characters without size
limitation.
Note: String class is given to overcome the limitations of char array.
The string class is declared as final to prevent it from being inherited(can’t form
subclasses).
the variable char array is declared as final, to make the String class object immutable.
String class object is immutable not because the String class is declared as final. But
because instance char array variable is declared as final.
A class whose instance members are declared as final is called an immutable class.
When we try to change the content of a String object, a new object is created and the
new content will be stored on this new object. If we do 1000 modifications to the content
of the String object content, 1000 new objects are created. Definitely, it will hampers the
performance of the application. To overcome this issue, the StringBuffer class is given.

When we try to change the content of the StringBuffer object, the modified content is
stored in the same current object. A new object is not created.
The class object that allows us to modify the content of that object and store that result
in the same current object is called Mutable object.

Whenever we create a String class object, in the background, the String object is created
with a char array variable. The array object is created with the number of locations equal
to the number of characters in the String object. The char array reference value is stored
in the String value variable. The String class object reference value is stored in the String
class reference variable.
Whenever we try to perform modifications to the content of the String object(by concat()
method for example), the concat() method will create a new object with a size equal to
the current object string literal length plus concat method argument string literal length.
Then it will copy the current object content and argument object content into a new char
array(concat operation will create the new char array. Because the old char array size is
fixed. But concat operation needs additional storage to add new data. So, it will create a
new array object). And new array object reference value is stored in the new String
object(Because of the concat operation, a new String object will be created. Because the
String object is immutable). Because the character array variable is final.
StringBuffer is mutable:

When we write a StringBuffer object creation statement, a precached memory(buffer) is


created with the size of 16 plus the size of the argument String literal of the StringBuffer
object.
Ex: for the StringBuffer  StringBuffer sb1 = new StringBuffer(“Lap”);,
The internal created buffer memory size is 16 + 3 = 19.

When we add additional data to the StringBuffer object, it will take the locations after the
previous data.
StringBuffer sb2 = sb1.append(“top”);

If all the locations of the array are filled up, then the new array with the size of double the
size of the previous array plus two is created. i.e., the previous array size is 19. The new
array size will be 2 * 19 + 2 = 40.

So, with the above explanation, we can say that StringBuffer is mutable. Because every
time we try to modify the StringBuffer data, a new object will not be created. The new
data will be added to the same object. But the array object is immutable. Because, if the
size of the array is full, then a new array will be created.

You might also like