String Interning is a method of storing only one copy of each distinct String Value, which must be immutable.
By applying String.intern() on a couple of strings will ensure that all strings having the same contents share the same memory. For example, if a name ‘Amy’ appears 100 times, by interning you ensure only one ‘Amy’ is actually allocated memory.
This can be very useful to reduce the memory requirements of your program. But be aware that the cache is maintained by JVM in permanent memory pool which is usually limited in size compared to heap so you should not use intern if you don’t have too many duplicate values
intern() method : In Java, when we perform any operation using intern() method, it returns a canonical representation for the string object. A pool is managed by String class.
- When the intern() method is executed then it checks whether the String equals to this String Object is in the pool or not.
- If it is available, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
- It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
It is advised to use equals(), not ==, to compare two strings. This is because == operator compares memory locations, while equals() method compares the content stored in two objects.
false true true
Explanation : Whenever we create a String Object, two objects will be created i.e. One in the Heap Area and One in the String constant pool and the String object reference always points to heap area object. When line-1 execute, it will create two objects and pointing to the heap area created object. Now when line-2 executes, it will refer to the object which is in the SCP. Again when line-3 executes, it refers to the same object which is in the SCP area because the content is already available in the SCP area. No need to create a new one object.
If the corresponding String constant pool(SCP) object is not available then intern() method itself will create the corresponding SCP object.
Explanation : We use intern() method to get the reference of corresponding SCP Object. In this case, when Line-2 executes s2 will have the value “GFGGFG” in it only creates one object. In Line-3, we try to intern s3 which is again with s2 in SCP area. s4 is also in SCP so all give output as true when compared.
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Collator compare(String, String) method in Java with Example
- Convert a Set of String to a comma separated String in Java
- Collator equals(String, String) method in Java with Example
- String Literal Vs String Object in Java
- Convert Set of String to Array of String in Java
- Insert a String into another String in Java
- Java.lang.string.replace() method in Java
- Java.lang.String.replace() in Java
- Java.lang.String class in Java | Set 2
- Java.lang.String.startswith() in Java
- Java.lang.String.copyValueOf() in Java
- Java.lang.String.matches() in Java
- Java.lang.String.getByte() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bhagat_dineshbe2006