File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object

 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sandeep Nachane
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic