• 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

hashcode

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
plzzz tell me the funda of hashcode....i studied khalid mughal but couldnt get much of it....
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey hi,
As far as i know , when you create a object of any class then hashcode() return the address on the heap where the object is created. This is by default. But thi is not applicable for Wrapper classes , For Integer wrapper class it is like

Integer i = new Integer(1);
i.hashcode();

it will return you 1.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hashcode() is used to uniquely identify the content referenced by an object with a integer number.
Its helpful when writing compilers based on java and in security related applications.

As far as the programmer is concerned, he / she needs to only
remember that calling an hashCode() on an object returns an unique
number that doesnt match with any other dissimilar instances.

For example:-
String str1 = "java";

String str2 = "java";

String str3 = "ranch";

String str4 = "ranch";

Then,
str1.hashCode() has a unique number and is the same and equal to str2.hashCode()

but not equal to str3.hashCode() or str4.hashCode() for obvious reasons.
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by abhrodip paul:
when you create a object of any class then hashcode() return the address on the heap where the object is created.



If hashcode() returns the address on the heap where the object is created
Is it guaranteed to return the unique result on multiple executions of your program? Is the object created at the same address in the heap all the times?
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

How many objects are created in above eg.
I think there are 4 objects created.
Also, Can you expalin the use of (==) method.
Please.
Correct me i am wrong

Thanks
Dinesh
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some body please clarify the doubts
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I will try my best to clear your all doubts...there are 3-4 questions here :

1) What Paul said is correct: Every object has its unique address (hashcodes) on heap
So String str1 = "java"; and String str3 = "ranch"; will have different addresses hence hashcodes on heap.


and we need something to reach objects on heap and that something is refrence variable of the class ( or of its super class) of which object we are trying to reach. Now we can have many refrence variables with different names but refrering to same object on heap.So
String str1 = "java";

String str2 = "java";

Both str1 and str2 are refering to same object on heap which is "java" .

Now the question is why no two different objects were created above ?
Because of m/m saving purposes , JVM maintains a pool of constant strings and wheneevr encounters any new refrence for these strings , just point them to alraedy existing constant strings in pool , now suppose if you write further
String str7 = "java"; , it will also refer to same object..


one more trick to remeber is this : whenevr you see object is being created thru new keyword that means new object will be creted as below :

String str1 = new String("java");

String str2 = new String("java");

Now strings str1 and str2 will have different addresses on heap and hence hashcodes...

2) About == : It returns true if two objects have same address..same expalination as above.

3)About Sheetal's doubt : Object creation is done at run-time , so everytime you run the program you will get different hash-code , depending on m/m on heap available and also on JVM mood



Hope this makes clear....

Thanku..
Vishal
reply
    Bookmark Topic Watch Topic
  • New Topic