• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String intern

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic