aspose file tools*
The moose likes Beginning Java and the fly likes 1 + 1 = 3? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "1 + 1 = 3?" Watch "1 + 1 = 3?" New topic
Author

1 + 1 = 3?

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 1 + 1 = 3?