• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

New challenge equals == hashcode(m confused)

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





output:-

false true true true true false true


Please explain the output and concept
 
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted Today 1:07:17 PM 0

posted Yesterday 11:17:32 PM private message
0
Quote
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/



a==c is true because a and c refers to the same strings in the string constant pool.
a, b, c, d has the same hashcodes because hashcode of a string is computated in the String class. For example String a = new String("a") ; String a1 = new String("a") ; a and a1 have the same hashcode.
a.equals( b ) is true because a and b have the same string.
But a == b is false because a refers to "Hello1234", but b refers to a string that is concatenated by "Hello" and new String("1234"). This is a new string object created when "Hello" and new String("1234") is concatenated.
Try this:
String a = new String("Hello1234");
String b = new String("Hello1234");
Will a==b be true?
I don't think so. a and b refers to two different string objects.

Try another one:
String a = "Hello1234";
String b = "Hello1234";
Will a == b be true?
Yes. I think so. a and b refers to the same string in the string constant pool.

a== d is false because a refers to a string in the string constant pool "Hello1234" and d refers to a new String object created from the character array.

 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when posting code samples. They make it much easier to read. I've added them for you here.

== compares object references to see if two things are occupying the same point in memory.
The equals() method contains specific ways of comparing different types of objects. Two Strings will be equal() (but not necessarily ==) if they contain the same sequence of characters.

That's an important distinction to understand. Really, you never use == to compare two Strings unless you're studying for a certification exam.

Now, the most confusing thing here is a == c being true, and a != c also true. You're right that is impossible, and here is just a matter of you misreading the output. The output I get is:

falsetrue false true true false true

It would be a good idea to print out markers, like a letter or number, or even the whole statement, so you can always match the right true/false with the statement that caused it.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic