aspose file tools*
The moose likes Beginning Java and the fly likes hash code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hash code" Watch "hash code" New topic
Author

hash code

rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
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 ???


Thanks
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
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

Joined: Aug 11, 2007
Posts: 259
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.


SCJP 5.0 | SCWCD 1.4
rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
thanks.... got the answer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hash code