This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

== operator not return true

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when


above code have to return true but return false when the string created using new operation but String s = "ranch" and String s1="ranch" the == return true

can any body please explain what is the internal
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

if you use == for comparing two Strings (or other objects) you compare if it is the same object, not if the content is the same. You should usethe equals() method.

See this for details:

how to compare string objects


As I said before: this is applicable to all objects.


John
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ G.Sathish
Refer Strings, Literally
 
Ranch Hand
Posts: 466
1
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this http://leepoint.net/notes-java/data/expressions/22compareobjects.html

Let me know if you have some issues.

Thanks
Vinod
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use ==, it actually checks if both the operands are pointing to the same object, if yes, it returns true.
When you use equals, it actually compares the content, and if same, it returns true.

Now when you use
String s = new String("ranch");
String s1 = new String("ranch") ;

Two string objects are getting created which are being refrenced by two different References that is s and s1.

Howevr, when you use
String s = "ranch"
String s1 ="ranch"

JVM internally creates only one object (Interned object).
So since there is only one object in this case and both the refrences are pointing to the same object, == is also returning true.

Hope you understood..
Please write back in case of any issues...

 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic