• Post Reply Bookmark Topic Watch Topic
  • New Topic

S1.equals(s2) both varibles s1 and s2 are having same string variables..but output is false  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S1.equals(s2) both varibles s1 and s2 are having same string value but the output i am getting is false ..under what conditions such thing is possible?
 
john sayeau
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S1.equals() or Object.equals is testing to see if both variables are pointing to the same object.

You could init 2 strings like this:
s1 = "a string"
s2 = "a string"
They have the same string value but s1 and s2 are references to two different objects so s1.equals(s2) would be false.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ummm...are you sure?

C:\slop>java Test
s1.equals(s2) is true


The String class has the equals() method defined to compare the contents of the Strings. Even if it didn't, due to the way strings work with the string pool, these two references both point to the same object, so it would still return true.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid this is incorrect John. String overrides the equals method to compare the values lexically. In your example s1.equals(s2) would return true.

Vishal, comparison of the two objects would return false if either of the two is not a String object. Maybe you are comparing a StringBuilder to a String?
 
john sayeau
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops...sorry.
I though it was the same as Object.equals.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

I Created this thread becaus of the false condiftion being raised in one of the servlets code

...but thanks to trim() problem has been resolved for the time being
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
...but thanks to trim() problem has been resolved for the time being

I suspect that there were extra spaces hanging around the string. So even an extra space means the string is different
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Vishal Hegde wrote:
...but thanks to trim() problem has been resolved for the time being

I suspect that there were extra spaces hanging around the string. So even an extra space means the string is different


Hi Mohamed Sanaulla,

I guess when adding values in table guess white spaces are there and it was troubling me since long time so in the end thought of using trim() and it worked and am relieved for the time being
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john sayeau wrote: . . . same as Object.equals.
When you go through the API documentation,m you find a set of "methods inherited from XXX". by looking at those methods you can easily tell whether the method is overridden or not.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
The String class has the equals() method defined to compare the contents of the Strings. Even if it didn't, due to the way strings work with the string pool, these two references both point to the same object, so it would still return true.

... true, with the caveat that it's only true when they're defined as literals in the code.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote: . . . only true when they're defined as literals in the code. . . .
. . . or when they are interned.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!