• 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

difference between string equals.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All
Can anyone please explain me what is the difference between
System.out.println(s2==s3);
System.out.println(""+(s3==s4));
I'm getting different answers for both of these...

Thnx
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What objects are s2, s3 and s4? Besides, one would need to know how you have initialized/assigned values to these objects before answering your question.

Regards,
Saket
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a test example below:

public class Test
{

public static void main(String[] args)
{
String s1="love",
s2="like";
System.out.println(s1==s2);
System.out.println(""+(s1==s2));
}
}

run java Test,the answer both are false except the second having a space aditonal.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following:


The difference has to do with how Strings are stored in memory in java and what the == operator does. The == test simply checks to see if the two objects in question have the same bit pattern or not. Remember that a variable contains the memory location of an object so that if two variables are set equal to each other, then they both point to the same location in memory and therefore have the same bit pattern. In java, a special section of memory is reserved as the "String pool." In this pool, java keeps a list of Strings that are created using the form

so that if I create another identical String such as

java simply sets s2 to point to the same place in memory that s1 is refering to in the String pool. This is done to save space in memory. Thats why prints "true." Now if I create a String using the "new" key word a new String object is created and is placed in on the heap which is in a different section of memory than the String pool. That's why prints "false" - because s2 and s3 contain references to different sections of memory. If you want to compare the values of the actual strings themselves, you must use the String method equals() which looks not at the memory locations but instead compares the Strings themselves. Get it?

[ December 16, 2004: Message edited by: J Williams ]

[ December 16, 2004: Message edited by: J Williams ]
[ December 16, 2004: Message edited by: J Williams ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic