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:-

  1. What is String?
  2. How to declare a String?
  3. Why String is immutable?
  4. What is a String Constant Pool?
  5. How to check that String objects have equal values?
  6. 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
  

The String reference variable value  changes whenever we change or assign a new String value. It doesn't change the existing object state. 'str' is pointing to the String "data" present  in String Constant Pool, later it is pointing to the String "car" present in String Constant Pool. Both Strings "data" and "car" are present in String Constant Pool. If we assign a new value to 'str', the reference of the old String object is replaced with the new reference. The value of the String object "data" created in String Constant Pool is not changed. That's why we called the String object immutable.

Following are the reasons why String is immutable:

1. Caching
2. Security
3. Thread Safety
4. Performance

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: 

  1. The String str1 Object is created inside heap memory. str1 refers to the memory location of heap memory.
  2. 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.
  3. Two objects are created when line 1 is executed.
  4. 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. 
  5. 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?

Let's suppose we want to check that String objects have equal values.
	String strg1="Test";
	String strg2="Test";
  	String strg3=new String("Test");
  	String strg4=new String("test");
In java, the String class provides some methods like equals(Object object) and equalsIgnoreCase(String stringObject). See below code:
	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
'strg1' and 'strg2' both refer to the same object present in SCP. whereas  'strg1' and 'strg3' both refer to different objects, 'strg1' refers SCP object while 'strg3' refers heap memory object.

Comments

Popular posts from this blog

Call By Value in Java