Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1 + 1 = 3?

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I consider myself a pretty learned java programmer but this really stumped me, I have no idea why this is:


When run the program returns: this equals this: false

How can that be? subStr and thisStr should be equal.
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can that be? subStr and thisStr should be equal.


The substring method always return a new string (if not the original string) -- so it is unlikely that it will ever be equal to another string by reference.

Now, if you mean it should be equal because this have the same value -- then you need to confirm equality with the equals() method. Not with the == operator.

Henry
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I consider myself a pretty learned java programmer but this really stumped me,

No offense, but did you learn how to compare Strings ? If not, check this Journal article.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok im gonna downgrade myself from pretty learned to just plain pretty
Ofcourse == compares two objects not their values, I forget that String is an object not a primative like it's smaller brother char
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic