In Java, the “intern()” method is used to ensure that if a large amount of string stores data then strings having the same element share the same memory. This method is very useful for reducing the memory space for the program. For instance, if you have a huge list of employee names where the name “jack” appears ten times. The “intern()” method will ensure that the “jack” occupies only a single space in the memory.
This post will discuss the usage of the String.intern() method in Java.
What is String.intern() in Java?
The “intern()” method in Java is utilized for returning the reference to the stated string object. However, if the string object existed with the same content/data in the string pool, then it will return the previous string’s reference. Furthermore, if the string does not exist in the string pool it will add the new string and its reference to the string pool.
Syntax
To utilize the stated method, just follow the syntax given below:
In this example, we will discuss why there is a need to use the “intern()” method in Java when the string is created in a heap using the “String()” constructor. First, a string object is created using the “String()” constructor. After that, the data for the string is passed as the parameter to this object and stored in a variable:
Another string object is created by following the same procedure:
Invoke the “println()” method and set “a=b” as the argument to this method. It will return the result as false because each string has its own space in the memory:
It can be noticed that the output of the above-stated example returns false:
Example 1: String.intern() Method to Check String Objects
In this example, two string objects are created by using the “String()” constructor and a string is passed to the constructor as an argument. Next, invoke the “intern()” method for each string and store the first string in variable “a” and the second one in the “b”. Check the condition “a=b” and use the “println()” to display the result:
As a result, it returns the “true” value because the “intern()” method ensures that each string occupied space in the same memory to store string data:
Example 2: String.intern() Method to Check String and String Object
In this stated example, various strings are created and stored in the different variables. For this purpose, first, a variable is initialized with a string value “Linuxhint”:
Utilize the “intern()” method by accessing the string “a” and store it in the variable “b”:
A new string-type object is created with the help of the “String()” constructor and passes the “Linuxhint” as the parameter to this constructor. Then, store it in the variable “c”:
Invoke the “intern()” method by accessing the variable “c” and store the result in the “d”:
Now, compare these created strings with each other. For that purpose, invoke the “println()” method and pass the parameter according to the below code snippet:
The output of each corresponding statement is printed on the console. We have mapped the output with each statement:
That’s all about the string.intern() method in Java.
Conclusion
The “String.intern()” method in Java returns the reference to the stated string object. However, if the string object existed with the same content/data in the string pool, then it will return the previous string’s reference. This post has stated various use cases of the String.intern() method in Java.