Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

String == compare doubt

 
Ranch Hand
Posts: 88
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,





why this returns false.Both hashCode are same???
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

The == operator checks if an object is "pointing to" the same reference "in memory".

When you do new String(...) a new reference is created in memory. Therefore the == operator returns false.

The hashcode() usually works together with the equals().

The equals() method checks the content of object (string) rather than the reference.

Hope this helps.
 
RatiKanta pal
Ranch Hand
Posts: 88
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tsang,
Correct me if i am wrong.hashCode give me exact address in equals methos.out side equals method what hashCode return.How do i get exact physical address of an object.

Thank you.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RatiKanta pal wrote:hi Tsang,
Correct me if i am wrong.hashCode give me exact address in equals methos.out side equals method what hashCode return.How do i get exact physical address of an object.



Short answer: you don't. But then you never need to.

The default hashCode() in java.lang.Object returns a value that is usually calculated using the physical address. But it's not actually the physical address (it can't be: it's only an int and that isn't big enough to describe the address space on a modern machine). Specifically, the documentation says:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)



But hashCode() needs to be consistent with equals(), so any class that overrides equals() should also override hashCode(). And the String class certainly does that - any two String objects containing the same value will have the same hash code.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic