• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String literal and String object comparison

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can i know the reason for the String literal assignment .

String str1 = new String("abc");
String str="abc";

System.out.println(str==str1);
System.out.println("Str memory-->"+str.hashCode());
System.out.println("Str1 memeory-->"+str1.hashCode());

output:
false
Str memory-->96354
Str1 memeory-->96354

Even though a new String object is created both of them have same address still str1=str gives false.
This is confusing .




 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we use the keyword "new" to create a string "abc" ,(String str1 = new String("abc)), it creates a new String object, whereas String str="abc" has no object.
If we want to compare these two strings, we use System.out.println(str1.equals(str)); ==> the out put will be "true"

Please read http://leepoint.net/notes-java/data/expressions/22compareobjects.html.

(ps. I am learning, any correction and tips are welcome)

vittal rao wrote:Hi,

Can i know the reason for the String literal assignment .

String str1 = new String("abc");
String str="abc";

System.out.println(str==str1);
System.out.println("Str memory-->"+str.hashCode());
System.out.println("Str1 memeory-->"+str1.hashCode());

output:
false
Str memory-->96354
Str1 memeory-->96354

Even though a new String object is created both of them have same address still str1=str gives false.
This is confusing .




 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hash code does not return the memory address. The hash code as returned by Object.hashCode() and System.identityHashCode() returns something that is possibly but not necessarily related to the memory address. However, classes can override the hashCode() method to return something completely different. In fact, the following class is perfectly fine:
String also overrides hashCode() but a [s]bit[/s] lot more sophisticated - it uses the actual characters for the hashCode() return value, and that is why both your strings have the same hashCode() return value even though they are not the same using ==.
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote: . . . In fact, the following class is perfectly fine . . .

Careful, Rob, they will believe you
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said fine, not good. It will behave badly in HashMaps and HashSets. Perhaps the correct word is "legal".
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means , since they both have same value, hence returning the same hashcode. Please confirm.

 
Campbell Ritchie
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you are familiar with the Object#hashCode() specification? Returning the same value from all instance via the hashCode method will satisfy the requirement that values returning true from equals() have the same hash code.

But it doesn't satisfy the recommendation that objects returning false from equals() return different hash codes if possible. So all objects of that class would go into the same bucket in a hash-based map or set.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic