Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String equvalence....

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

plz refer the code below:


the o/p will be false, but as we know that two entirely different objects can have same hashcode value, why is it that we always get false as o/p here.

thanx
amit
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Different objects even of the same class (and of the same value) gives different hashcodes.

Please refer to the hashcode method API in this Sun API Specs page.



the o/p will be false, but as we know that two entirely different objects can have same hashcode value, why is it that we always get false as o/p here.

thanx
amit[/qb]<hr></blockquote>
[ April 17, 2005: Message edited by: Ken Loh ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For StringBuffer, it is special case and it doesn't override the equals mtd so anytime ".equals()" will print false for two StringBufferStringBuffer with same String value .

Thanks
[ April 17, 2005: Message edited by: Kalyani Marathe ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when we write the following statement where A and B are not of primitive data types
A == B

then if A and B points to the same object then it will return true else it will return false. i.e. == operator does not comapre the values of the objects rather they check the reference values of A and B. if A and B are alias of each other only then == operator will return true.

Consider this
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = s1;

then s1 == s2 returns false ( s1 and s2 refer different String onject )
s1 = s3 returns false
s2 = s3 returns true as both of them points to the same string object (s2 and s3 are aliases )
 
Manish Nijhawan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry for the mistake the example which i have given should be
Consider this
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = s2;

then s1 == s2 returns false ( s1 and s2 refer different String onject )
s1 = s3 returns false
s2 = s3 returns true as both of them points to the same string object (s2 and s3 are aliases )
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Different objects even of the same class (and of the same value) gives different hashcodes.


This is not true.
In fact, it is impossible to achieve unless construction is controlled.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ken]: Different objects even of the same class (and of the same value) gives different hashcodes.

They can give different hashcodes for objects the same class and field valuess - as shown by your example with StringBuffer. However it's not guaranteed. And for many other classes (such as String) it will never happen. If a class overrides HashCode() in a way that depends on the field values and nothing else (which is what most do, I think) then objects of the same class with the same field values will always have the same hashCode().
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim & Tony, thanks for keeping me on the right track !
Ken

Originally posted by Jim Yingst:
[Ken]: Different objects even of the same class (and of the same value) gives different hashcodes.

They can give different hashcodes for objects the same class and field valuess - as shown by your example with StringBuffer. However it's not guaranteed. And for many other classes (such as String) it will never happen. If a class overrides HashCode() in a way that depends on the field values and nothing else (which is what most do, I think) then objects of the same class with the same field values will always have the same hashCode().

 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit

As Kalyani told StringBuffer class is a special case where equals() and hashCode() methods are not implemented. So even though there is a chance to get same hashcode from the different objects of same class, it is not possible in the case of StringBuffer.

Thank You.
 
You showed up just in time for the waffles! And this 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