Storage of String in Java


A string is a class in Java that stores a series of characters. Those characters are actually String-type objects. The value of string is enclosed within double quotes. The string class is available in java.lang package.

In this article, we will look into the storage mechanism of strings in java.

String and Storage of String

To create strings −

Syntax

String nameOfobject = “ values ”; 

Instance 1

String st1 = “Tutorix and Tutorialspoint”; 

Here, ‘st1’ is the reference variable and its values are in double quotes.

We can also use new keyword to create new string −

Syntax

String nameOfobject = new String(“ values ”); 

Instance 2

String st2 = new String(“You are here at Tutorialspoint”);

Here, ‘st2’ is the reference variable created using new keyword.

To create a string from character array −

Instance 3

char chs[] = { ‘T’, ‘U’, ‘T’, ‘O’, ‘R’, ‘I’, ‘X’ };
String st3 = new String(chs);

Here in the above example, we have created character array ‘chs’ and passed them in string ‘st3’ as an argument using new keyword.

Till now we have discussed what is string and how we can create strings in numerous ways. Now, let’s understand how it is stored in memory.

String Pool

Storage of strings in java is different from other programming languages. We all know that the reference variables are stored in stack and their values are stored in heap. Same happens in the case of Strings, the reference variables are stored in stack but their objects along with given values are stored in a string pool which is a separate memory space inside the heap.

When we create a string-type object it gets stored in string pool of heap. At this point string pool is created in heap. Now, whenever we create a new string with the same value or without same value the JVM checks whether the given value is already available in string pool or not. If it is not available then new value is created for that reference variable but in opposite case, JVM does not recreate new object with given value it simply points the old value to that reference variable.

Example

Suppose we are creating a string object as −

String st4 = “Tutorix”;

Then, it will be stored in string pool −

When we create a new String object −

String st5 = “Tutorials”; Then,

But, If we initialize the st5 to “Tutorix” then JVM doesn’t create new object for it. It points the older value to the ‘st5’.

Program to check Storage of String

We can check whether the two reference variables are pointing to the same object or not using comparison operator ‘==’. It returns true when two object references refer to the same instance otherwise false.

Example 1

In this example, we will declare and initialize two string variables ‘st1’ and ‘st2’ with same value. Then, we will use == operator to check whether they are pointing to same object or not. The result will be stored in a Boolean variable ‘isSame’.

public class Main {
   public static void main(String[] args) {
      String st1 = "Tutorix"; 
      String st2 = "Tutorix"; 
      boolean isSame = (st1 == st2);
      System.out.print(isSame);
   }
}

Output

true

Example 2

In this example, we will create two string objects using new keyword. When we use the new keyword JVM creates a new object irrespective of the value of given object. In other words, JVM will create a new object for given reference variable whether the value is same or not.

public class Main {
   public static void main(String[] args) {
      String st1 = new String("Tutorix"); 
      String st2 = new String("Tutorix"); 
      boolean isSame = (st1 == st2);
      System.out.print(isSame);
   }
}

Output

false

In the above code, we have created two strings using new keyword and again we have checked whether they are pointing to same object or not using == operator. The result is false here which signifies that both are pointing to different objects.

Conclusion

In this article, we have understood strings and how they are stored in memory. Also, we checked whether two reference variables with same values are pointing to the same object or not through java programs.

Updated on: 05-May-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements