STRING INSTANCE METHODS
Java String intern() method
intern() returns the canonical string from the String Pool. If equal text already exists in the shared string pool, Java can return a reference to that shared object.
Your first intern() call
String pirmas = new String("Java");
String antras = pirmas.intern();
String trecias = "Java";
System.out.println(pirmas == antras);
System.out.println(antras == trecias);
System.out.println(pirmas.equals(trecias));false
true
true
new String("Java")creates a separateStringobject.pirmas.intern()returnsString Poolcanonical value from the pool, Java.treciasis a string literal, so it also uses theString Pool.pirmas == antrasisfalsebecause these are different references.antras == treciasistruebecause both point to the same interned value.equals()compares contents, sopirmas.equals(trecias)istrue.
Method syntax
public String intern()
| Part | Meaning |
|---|---|
| Calling object | A specific String, for example, tekstas.intern(). |
| Parameters | No parameters. |
| Return type | String – the canonical string from the String Pool. |
| If the value is already in the pool | Returns the existing value. |
| If the value is not in the pool | Adds it to the pool and returns its reference. |
Important: intern() does not change the text. It obtains a shared, JVM-managed object for the same string value.
Why is intern() an instance method?
intern() is called on a specific string because Java must know which value to look for in the String Pool pool:
String isPool = tekstas.intern();
String tekstas = new String("JavaQuest");
String isPool = tekstas.intern();
System.out.println(isPool);JavaQuest
We therefore do not write String.intern(...). The method must be called on the specific String value whose canonical form is sought.
The string pool and string literals
String pirmas = "Java";
String antras = "Java";
System.out.println(pirmas == antras);
System.out.println(pirmas.equals(antras));true
true
When you write a string literal, such as "Java", Java uses the String Pool. If that value is already in the pool, the new literal can reference the same object. Therefore, in this example, pirmas == antras returns true.
new String() creates a separate object
String pirmas = new String("Java");
String antras = "Java";
System.out.println(pirmas == antras);
System.out.println(pirmas.equals(antras));false
true
new String("Java") creates a new object, so its reference differs from the literal's "Java" reference. Their contents match, however, so equals() returns true.
intern() obtains the shared pool reference
String pirmas = new String("Java");
String antras = "Java";
String internuotas = pirmas.intern();
System.out.println(internuotas == antras);
System.out.println(internuotas.equals(antras));true
true
When pirmas.intern() returns String Pool value, it matches the literal "Java". Therefore, internuotas == antras is true. In ordinary code, still compare string contents using equals().
Text created at runtime
String dalis1 = "Java";
String dalis2 = "Quest";
String sujungtas = dalis1 + dalis2;
String literalas = "JavaQuest";
System.out.println(sujungtas == literalas);
System.out.println(sujungtas.intern() == literalas);false
true
sujungtas is built from variables, so it is not the same object as the literal "JavaQuest". After sujungtas.intern() , the canonical pool value is obtained and its reference matches the literal.
equals() remains the main choice for comparing text
String ivestis = new String("ADMIN");
String leidimas = "ADMIN";
if (ivestis.equals(leidimas)) {
System.out.println("Turinys sutampa");
}
if (ivestis.intern() == leidimas) {
System.out.println("Ta pati pool reikšmė");
}Turinys sutampa
Ta pati pool reikšmė
intern() can make two equal strings reference the same pool object. That does not mean you should use ==everywhere. Compare user input, database values, API responses and ordinary business strings using equals() or equalsIgnoreCase().
The canonical value rule
String a = new String("LT");
String b = new String("LT");
String c = new String("LV");
System.out.println(a.intern() == b.intern());
System.out.println(a.intern() == c.intern());
System.out.println(a.equals(b));true
false
true
If two strings have equal contents, their intern() results refer to the same canonical value. Different contents produce different interned values. Equal contents can therefore share one pool representation.
intern() does not change the original String
String tekstas = new String("Java");
String isPool = tekstas.intern();
tekstas = tekstas.concat("Quest");
System.out.println(isPool);
System.out.println(tekstas);Java
JavaQuest
String is immutable in Java. intern() returns a pool value without changing the original object's contents. When the code later creates JavaQuest, that is a new string value.
The method cannot be called on null
String tekstas = null;
System.out.println(tekstas.intern());NullPointerException
intern() is an instance method. If a variable holds null, it refers to no String object, so Java has nothing on which to call the method and throws NullPointerException.
Common mistakes
==is used for ordinary string comparison instead ofequals().- Assuming that
intern()changes the original string, although it returns a pool value. new String(...)is used unnecessarily when a simple literal is usually better.- Interning many distinct values, which can burden the
String Pool. - Forgetting to check
nullbefore callingintern(). - Concluding that simply using
intern()improves speed, when it is usually a specialized memory optimization.
When should you use intern()?
intern() Use it cautiously when you have many repeated strings and understand why a shared canonical value is needed. Beginners should mainly understand why == and equals() with String can produce different results. For everyday string comparison, generally choose equals().