wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object" Watch "Object" New topic
Author

Object

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
public class MyClass
{
public static void main(String args[])
{

Object obj1= "SCJP" ;
Object obj2 = new String("SCJP");
System.out.println(obj1.equals(obj2));
}
}
In this code it prints out true when i supposed it should print false.
if we change the string matter in obj1 then it prints out false.
Why should the compiler relate it to the Strings. What i think it is supposed to be just compare based on references ie Object.Plase help me.
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61

Why should the compiler relate it to the Strings. What i think it is supposed to be just compare based on references ie Object.

Shabbir.
Equals operator is supposed to work on the contents of the object. The '==' operator compares the references only. So if you would have used

It would have printed falseonly.
hope it helps
Gaurav Mantro
------------------
http://www.mantrotech.com


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
Shabbir,
I think I recognize the code from my Object Equivalence Self Review Test
You asked "why does the compiler relate to String" and the reason is String object is assigned to Object reference in the code. Compiler knows this and hence equals() method of String is used and not of Object
_Sandeep Nachane


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
HI Sandeep!
Thanks a lot. I pasted this code from your site. It's avry good site to clear the concepts. I have one more code for you. I will be thankful if you please clear it out.
public class MyClass
{
public static void main(String args[]) {
StringBuffer strlit = new StringBuffer("SCJP");
StringBuffer strobj = new StringBuffer("SCJP");
System.out.println(strObj.hashCode() == strobj.hashCode());
}
} this code prints out false. It means it calls hashCode method defined in the object class. Now if you call this method on String objects it will printout true. Why this is happening
Navin Narayan
Greenhorn

Joined: Jan 25, 2001
Posts: 25
Hi Shabbir,
Most often, it can be assumed that hashcode() gives an int which is related to the memory address. (Even though it is not necessary for every java machine implementation)
So in this case two objects have been created. Both have unique memory addresses. So their hashcode is different.
Hope that answers your question
Navin.
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
<code>
public class MyClass
{
public static void main(String args[]) {
StringBuffer strlit = new StringBuffer("SCJP");
StringBuffer strobj = new StringBuffer("SCJP");
System.out.println(strObj.hashCode() == strobj.hashCode());
}
}
</code>
For JLS, if two object are equal according to equals method, then their hashcodes are needed to be equal.
For StringBuffer, it has not overrided the equals method, so two different objects will have equals method return false.
For String, it has overrided the equals method, so the two objects in the above code will have equals method return true.
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
I have one more code for you. I will be thankful if you please clear it out.
public class MyClass
{
public static void main(String args[]) {
StringBuffer strlit = new StringBuffer("SCJP");
StringBuffer strobj = new StringBuffer("SCJP");
System.out.println(strObj.hashCode() == strobj.hashCode());
}
} this code prints out false. It means it calls hashCode method defined in the object class. Now if you call this method on String objects it will printout true. Why this is happening

The answer to your question is:
The default implementation of hashCode() method in the Object class returns a value that is unique for different objects. Since all objects inherit methods from Object class, either directly or indirectly, you get the default implementation unless you override the hashCode() method in your class. The hashcode() method is overridden in String class but not in StringBuffer class.
Hope this helps !
-Sandeep Nachane
Vegad Arvind
Ranch Hand

Joined: Jan 10, 2001
Posts: 42
Hi,
Remember :
if u compare any two objects with equals() and if it prints true then definetly hascode() prints true.

Avi
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
i wonder if what i am thinking is right:
first: there is a error in the code strobj, strObj..it shd be strobj, strlit....am i right?
assuming that i wd answer as follows:
the string class creates strings which are immutable. so
String s1="ttt";
String s2="ttt";
creates 2 string object references which POINT to the same literal "ttt".
in stringbuffer however, every stringbuffer object points to a DIFFERENT literal.
therefore the hashcodes for the stringbuffer objects will be different.
strbuff1 and strbuff2 point to to different literal ("ttt"), thus have different addresses thus different hashcode...
whew!
am i right??
 
jQuery in Action, 2nd edition
 
subject: Object