String Class in Java
String class in Java
This blog is related to the String class present in java.
The following points are covered below:-
- What is String?
- How to declare a String?
- Why String is immutable?
- What is a String Constant Pool?
- How to check that String objects have equal values?
- How to compare two String references?
What is String?
Java provides a String class to manipulate the sequence of characters. The sequence of characters value is considered a String. Once a String object is created, we are not able to change its state. In other words, we can say that It is immutable.
String test="test";
//is equivalent to
char c[]={'t','e','s','t'};
String class
public final class String extends Object
implements Serializable, Comparable<String>, CharSequence
How to declare a String?
//By using a new keyword
String s1=new String();
//String literal
String s2="Test";
//Instantiated object by using Constructor
String s3=new String("Hello");
Why String is immutable?
Immutable means once it is created we can't change it. The state of an object is not changed once it is instantiated. String class is final so that no class will extend String class and it is using String constant pool to store String literals.
String str="data";
System.out.println(str); // data
str="car";
System.out.println(str); // car
What is a String Constant Pool?
Java uses heap memory to store objects. String Constant Pool is a special memory area inside a heap memory to store String literals. String Constant Pool is used to optimize the memory performance.
The concept of using SCP is that if we have so many duplicate String values it will impact the memory, Strings are stored as an object in String Constant pool.
![]() |
Figure-1: Memory Allocation String |
Whenever a String Literal is created, Firstly JVM checks whether the String with the same value is already present inside a String Constant pool or not. If it is not present, It will create an object in String Constant Pool and return a reference and If it is already present in String Constant Pool then return a reference of an already existing object.
For example:
String str1="Test";
String str2="Test";
System.out.println(str1==str2) //true
In the above example, There are two Literals with the same value "Test". Both str1 and str2 refer to the same object present inside the String Constant Pool. Thus both references refer to the same memory location.
![]() |
Figure-2: |
There is a catch here all the Strings are not stored in the String Constant Pool. The String object created with the 'new' keyword is stored in heap memory.
For Example:
String str1=new String("hello");
In the above example, the String object 'str1' is created in heap memory with the help of the 'new' keyword.
String str1 = new String("hello"); // Line 1
String str2= str1.intern(); // Line 2
System.out.println(str1==str2);//false
![]() |
Figure-3 : |
So, let's understand what is happening here in the following steps:
- The String str1 Object is created inside heap memory. str1 refers to the memory location of heap memory.
- In the String Constructor, we are passing "hello' as an argument, It checks whether "hello" is present or not in SCP. If not present, It will create an object in SCP or If already exists, It returns the reference.
- Two objects are created when line 1 is executed.
- In line 2, when str1.intern() called it fetches the reference from the String Constant Pool. Thus, str2 refers to the memory location of SCP.
- When we check String objects with the (==) operator, It will check the memory location. 'str1' and 'str2' refer to the different locations.
How to check that String objects have equal values?
String strg1="Test";
String strg2="Test";
String strg3=new String("Test");
String strg4=new String("test");
System.out.println("Values of strg1 and strg2 are equal : "+strg1.equals(strg2));
System.out.println("Values of strg1 and strg3 are equal : "+strg1.equals(strg3));
System.out.println("Values of strg1 and strg4 are equal : "+strg1.equals(strg4));
System.out.println("Using equalsIgnoreCase method ,Values of strg1 and strg4 are equal : "+strg1.equalsIgnoreCase(strg4));
Output:
Values of strg1 and strg2 are equal : true
Values of strg1 and strg3 are equal : true
Values of strg1 and strg4 are equal : false
Using equalsIgnoreCase method ,Values of strg1 and strg4 are equal : true
How to compare two String references?
By using the (==) operator we can compare String references.
String strg1="Test";
String strg2="Test";
String strg3=new String("Test");
System.out.println(strg1==strg2); // true
System.out.println(strg1==strg3); // false
Comments
Post a Comment