• 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

one more with Strings ( == and equals)

 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please observe the following code:
String a = new String("java");
String b = new String("java");
System.out.println(a == b) // this prints false
System.out.println(a.equals(b)); // prints true
String c = "java";
String d = "java";
System.out.println(c == d) // this prints true
System.out.println(c.equals(d)); // prints true
Can you please explain me the reason for this?
thanks!
Murthy
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Murthy
String a = new String("java") creates a String object. System.out.println(a == b) is false as it does shallow comparison. It compares Objects instead of the content of the object.
The method equals() does deep comparison. It compares the content of the String. Thus,
System.out.println(a.equals(b)) prints true.
As for
String c = "java";
String d = "java";
since there's no "new" used, it does not create a new object instead is pointing to the same string in the string pool. Since it's the same string, == will return true.
I hope this brief explanation helps a bit. You should do a search here. This topic has been discussed a lot here. I'm sure the explanations provided by the other ranchers are much better.
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karen, thanks for your nice explanation.
Hence, Can I say that, whenever we use "new" to create the same string ( like "java" in this example), == always returns FALSE as it compares the objects.
please correct me , if I am wrong and give some example.
[ June 04, 2002: Message edited by: Murthy Kompella ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murthy Kompella:
Hence, Can I say that, whenever we use "new" to create the same string ( like "java" in this example), == always returns FALSE as it compares the objects.


That is correct...initially. The String class has a method called "intern" which allows you to reuse Strings from the pool. Take a look at this example:

In short, however, whenever you use the keyword "new," a new object is created which, therefore, has a unique reference. As such, using "==" on it with any other reference will return false.
Corey
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's a nice explanation.
In addition to that, just to share with everybody,
I just referred java Api for Intern , which says

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic