Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hash code

 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain me the output....



public class Main {

public static void main(String[] args) {

Object o = new Object();
Object o1 = new Object();

Main m = new Main();
Main m2 = new Main();
m2=m;
System.out.println(m.toString());
System.out.println(m2.toString());

System.out.println(m.getClass().getName()+'@'+ m2.hashCode() );
System.out.println(m.getClass().getName()+'@'+ m.hashCode() );
}
}


output

init:
deps-jar:

compile:
run:
Main@3e25a5
Main@3e25a5
Main@4072869
Main@4072869



why is the output different when we use tostring() method and hashcode() method ???
 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In definition of Object method toString you have:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Ref to Java Doc
[ August 21, 2007: Message edited by: Kuba Zygmunt ]
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!
if you you the API
you will see that the method tostring() of the superclass object is:



It exacly what your doing in System.out.println().

That why you have the same result.
 
rahul mehra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.... got the answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic