• 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

String variable in hashCode method

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is comparing "what" exactly ??

WP
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
And who’s Jasper?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic