*
The moose likes Beginning Java and the fly likes Strings behaving in abnormal manner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings behaving in abnormal manner" Watch "Strings behaving in abnormal manner" New topic
Author

Strings behaving in abnormal manner

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Friends,
see the following code & output:
class Abcd
{
public static void main(String[] args)
{
String s1="seed";
String s2="seed";
System.out.println(s1.hashCode()+" "+s2.hashCode());
System.out.println(s1==s2);
String s3=new String("SEED");
String s4=new String("SEED");
System.out.println(s3.hashCode()+" "+s4.hashCode());
System.out.println(s3==s4);
System.out.println("Hello World!");
}
}


output:
3526257 3526257
true
2541169 2541169
false
Hello World!

Press any key to continue...


Don't you think that line
System.out.println(s3==s4);
should print "true" but it is producing "false" even if the hashCode is same.

Any logic behind this?

Regards,
Shrinivas
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
You should compare Strings (and Objects in general) using 'equals', not using '=='. equals tests equality of the object values, while == tests equality of object references. Even for non-mutable, seemingly identical, objects like the ones in your example, the references may or may not be the same.


Ping & DNS - my free Android networking tools app
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
in other words, the behaviour is quite normal


42
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Two distinct objects that are unequal (according to the Object.equals(Object) method) may produce the same hash codes (according to the Object.hashCode() method). In fact, unless construction is controlled (which best practices mandates anyway, but common practice ensure that it is prolific), it's impossible to enforce - all you need to do is construct 2^32 + 1 unequal instances and a hash code collision is guaranteed.

This may be all beside the point, since your question is unclear, in which case, ignore it.


Tony Morris
Java Q&A (FAQ, Trivia)
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Thanx Everybody,

One more fact is in String hashCode() is overridden & it do not represent memory address but hashCode depends on String Contents.string (2 different objects ) can have same hashcode provided that their contents are same.


Shrinivas
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One more fact is in String hashCode() is overridden & it do not represent memory address but hashCode depends on String Contents.string (2 different objects ) can have same hashcode provided that their contents are same.

Indeed. Objects that are equal *should* generate the same hash code.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Shrinivas Mujumdar:
string (2 different objects ) can have same hashcode provided that their contents are same.


They *must* have the same hashcode if they are equals, and they *may* have the same if they are not equal.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

They *must* have the same hashcode if they are equals
Per the contract, absolutely. There was no reason for me to shy away from the stronger term.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings behaving in abnormal manner
 
Similar Threads
System.out.println(S1==S2)????????
Help please
one more on ==
Two ways to construct a String/pool of strings problem
String