• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic