wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String intern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String intern" Watch "String intern" New topic
Author

String intern

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

This is a program from wrox jdk1.5 book ..regarding string comparison

*********************************************

public class MatchStrings {
public static void main(String[] args) {
String string1 = "Too many ";
String string2 = "cooks";
String string3 = "Too many cooks";
// Make string1 and string3 refer to separate strings that are identical
string1 += string2;
// Display the contents of the strings
System.out.println("Test 1");
System.out.println("string3 is now: " + string3);
System.out.println("string1 is now: " + string1);
if(string1 == string3) // Now test for identity
System.out.println("string1 == string3 is true." +
" string1 and string3 point to the same string");
else
System.out.println("string1 == string3 is false." +
" string1 and string3 do not point to the same string");
// Now make string1 and string3 refer to the same string
string3 = string1;
// Display the contents of the strings
System.out.println("\n\nTest 2");
System.out.println("string3 is now: " + string3);
System.out.println("string1 is now: " + string1);
if(string1 == string3) // Now test for identity
System.out.println("string1 == string3 is true." +
" string1 and string3 point to the same string");
else
System.out.println("“string1 == string3 is false. " +
" string1 and string3 do not point to the same string ");
}
}
**************************************************************

And the explanation offered is ...

The three variables string1, string2, and string3 are initialized with the string literals you see. After executing the assignment statement, the string referenced by string1 will be identical to that referenced by string3, but as you see from the output, the comparison for equality in the if statement returns false because the variables refer to two separate strings. Note that if you were to just initialize string1 and string2 with the same string literal, “Too many cooks”, only one String object would be created, which both variables would reference. This would result in both comparisons being true. Next you change the value of string3 so that it refers to the same string as string1. The output demonstrates that the if expression has the value true, and that the string1 and string3 objects do
indeed refer to the same string. This clearly shows that the comparison is not between the strings themselves, but between the references to the strings.

QQQQ

When the concatenation occurs ...the literal "Too many cooks" is already there in the pool,so doesnt the intern function see to it that this also references the same literal.

Thnx in advance,
Aneesh
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

The reason is that ...........

All string constants and constant String expressions are automatically interned.

but for ojbects there is a need for explicit calling to be done..

string1 = string1.intern();

Thnx
 
Consider Paul's rocket mass heater.
 
subject: String intern
 
Similar Threads
Replace words in a file by reading a properties file
Check String for null without error?
String equality
Question about StringBuffer efficiency
WHEN ARE TWO STRINGS EQUAL?