A Quick Guide to Java String intern() Method and Explained with examples. String intern method called automatically for every string literal creation.
1. OverviewIn this post, We'll learn String intern() method in java with examples and This is part of java.lang package. Intern method is invoked automatically on every string literal creation.
String is immutable that means a new String object will be created for every operation or invoking any method on string object. In another words, Original String content will not be modified.
2. String intern()This method returns a canonical representation for the string object from string pool.
String maintains private pool for string literals. Initially this pool will be empty. Once any string is created, first checks in pool for the same object exists or not using equals method.
If it is present then it returns its reference. Otherwise, this String object is created and added to the pool and also a reference to this String object is returned.
public String intern()
2.2 ParametersNo parameters
2.3 Return TypeThis method returns String which is from string private pool.
For example, if we create any string object multiple times for same content then string intern method make sure only string object will be created only once in private pool.
3 String EqualityString Equality checks if and only if two string intern() method returns same string reference then only str1.equals(str2) is true.
String str1 = "Welcome"; String str2 = "Welcome"; System.out.println(str1.equals(str2));
4. String Intern ExamplesExample program on String intern method. We'll will write few example programs on this.
4.1 intern method example 1
String str1 = "Welcome"; String str2 = "Welcome"; String internStr1 = str1.intern(); String internStr2 = str2.intern(); System.out.println("str1 : "+str1+", internStr1 : "+internStr1); System.out.println("str2 : "+str2+", internStr2 : "+internStr2);Output:
str1 : Welcome, internStr1 : Welcome str2 : Welcome, internStr2 : Welcome
In fact, intern method returns string from its pool and presents contents in canonical form. But, we will see as normal string.
4.2 intern method example 2 for equality
String str3 = "java"; String str4 = str3.intern(); String str5 = "w3schools"; String str6 = str5.intern(); System.out.println("str3 == str4 : "+(str3 == str4)); System.out.println("str5 == str6 : "+(str5 == str6));
str3 == str4 : true str5 == str6 : true
Take a look at the above code and First creating string literals. Next, calling intern() method on the them.
Later comparing original string literal with interned string references. It printed true for both comparison. That means both are pointing to the same address location in the memory.
4.3 intern method example 3 for String creation with "new" keyword
Observe the below code. Comparing string literal references. Next, comparing string objects created with "new" keyword with interned objects.
String str7 = "java-w3schools"; String str8 = str7.intern(); String str9 = new String("java-w3schools"); String str10 = str9.intern(); System.out.println("str7 == str8 : "+(str7 == str8)); System.out.println("str7 == str9 : "+(str7 == str9)); System.out.println("str7 == str10 : "+(str7 == str10)); System.out.println("str9 == str7 : "+(str9 == str7)); System.out.println("str9 == str8 : "+(str9 == str8)); System.out.println("str9 == str10 : "+(str9 == str10));
str7 == str8 : true str7 == str9 : false str7 == str10 : true str9 == str7 : false str9 == str8 : false str9 == str10 : false
In this tutorial, We've seen how to retrieve the canonical representation of string from it's private pool using intern() method. Illustrated intern() method with example programs.
Among all String methods, intern() method only does not create a new String object unless it is not available in the string constant pool.
Also found that intern() is native method which may be implemented c or c++.
Example code snippets shown in this article is available on GitHub.