• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String variable in hashCode method

 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Could you please tell me how to use the instance variable 'name' in the hashCode method in the below example.
or is it that only int/byte/long data types should be used in hashCode?
The code works fine.


 
Winston Gutkowski
Bartender
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod talekar wrote:Could you please tell me how to use the instance variable 'name' in the hashCode method in the below example.

hashValue = 31* hashValue + name.hashCode();

or is it that only int/byte/long data types should be used in hashCode?

Absolutely not. The hashCode() method should contain everything that is used to determine equals().

The code works fine.

Hmmm. Up to now, methinks.

Winston
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is comparing "what" exactly ??

WP
 
Winston Gutkowski
Bartender
Pie
Posts: 10505
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:is comparing "what" exactly ??

Well spotted that man. +1.

I was too busy answering the question.

Winston
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is comparing "what" exactly ??

Guys, I didn't understand this question. Could you please explain it.

It's comparing the name variables of two objects.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at articles about equals and hashCode. One place is Effective Java by Bloch, which you can find an old edition of here. Note the bit about 13 17 19 has changed to 31. All those numbers are primes, so as to get the most variability in the less‑significant bits of the hash codes. The smaller the difference in hash codes, the more effective it is, as long as it’s not 0!
Also Google for Angelika Langer Java hashCode equals and look at this document by Odersky spoon and Venners.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod talekar wrote:
is comparing "what" exactly ??

Guys, I didn't understand this question. Could you please explain it.

It's comparing the name variables of two objects.

No, it's not. When you compare two strings using ==, Java is not going to check if the strings have the same content - it's only going to check if the variables on both sides of the == refer to the exact same String object. If you have two separate String objects with the same content, the result is going to be false.

Use equals() to compare strings: setTest.name.equals(this.name)
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell & Jesper.(corrected)

== means object reference equality and equals() means object value equality or meaningfully equal.

But don't understand what William is trying to point out by writing
is comparing "what" exactly ??

Jesper, I should have written in the overriding equals() method as you have pointed out as they have to be meaningfully equal.
and as Winston noted,
But then the next question is , why the code printed '1, pramod' just once earlier, when I had written 'setTest.name == this.name;' in equals() method?
As I had made a mistake in equals() method, it should have always written as false.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
And who’s Jasper?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pramod talekar wrote:But then the next question is , why the code printed '1, pramod' just once earlier, when I had written 'setTest.name == this.name;' in equals() method?
As I had made a mistake in equals() method, it should have always written as false.

To prevent a proliferation of redundant String objects using up memory, Strings are stored in a pool. When you use a String literal in your code, as you have with new SetTest(1,"pramod"), Java checks the String pool first to see if a String object with this value already exists. If it finds the required object, it will use that instead of instantiating a new one. So in this case, == returns true because both sides of the expression happen to be using the same object from the String pool. However, not all Strings are kept in the pool. The boolean expression "cyan" == new String("cyan") will return false!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic