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

String variable in hashCode method

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

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.




Thanks,
Pramod
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860



is comparing "what" exactly ??

WP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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

Joined: Apr 26, 2010
Posts: 316

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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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)


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

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

Joined: Oct 13, 2005
Posts: 36514
    
  16
You’re welcome
And who’s Jasper?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String variable in hashCode method
 
Similar Threads
Duplicated rows on the database
HashSet allowing duplicates; please explain
HashSet and equals()
Natural Ordering in HashSet for objects
Using java.util.HashSet for Custom Class