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.
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()
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.
String str1 = "Welcome"; String str2 = "Welcome"; System.out.println(str1.equals(str2));
Output:
true
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.
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));
Output:
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.
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));
Output:
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.
No comments:
Post a Comment
Please do not add any spam links in the comments section.