javaagile
asked on
java string equality
The result of executing this code will print '2' but why?
String a = "foo";
String b = "food".substring(0, 3);
String c = b.intern();
if (a.equals(b)) {
if (a == b) {
System.out.println("1");
} else if (a == c) {
System.out.println("2");
} else {
System.out.println("3");
}
} else {
System.out.println("4");
}
a) Because "foo" is not equal to "food".substring(0,3) using equals().
b) Because the intern method returns a canonical reference to the string, which just so happens to be the same as variable a, because a is a constant.
c) Because you must use the .equals function to compare Strings in Java.
d) Because the compiler can tell that the strings will be equal and works it out for you at compile time.
String a = "foo";
String b = "food".substring(0, 3);
String c = b.intern();
if (a.equals(b)) {
if (a == b) {
System.out.println("1");
} else if (a == c) {
System.out.println("2");
} else {
System.out.println("3");
}
} else {
System.out.println("4");
}
a) Because "foo" is not equal to "food".substring(0,3) using equals().
b) Because the intern method returns a canonical reference to the string, which just so happens to be the same as variable a, because a is a constant.
c) Because you must use the .equals function to compare Strings in Java.
d) Because the compiler can tell that the strings will be equal and works it out for you at compile time.
We're not allowed/here to do your homework. Sorry.
ASKER
this is not homework. I need to understand how intern() method on string object works? value of a is "foo" and value of b is "foo" and value of c is also "foo". But why a==c is false and a==b is true?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for responding. I also had a look at javadoc but still could not understand because in code it makes c=b.intern() then why a==c is true? I understand that if c=a.intern() then a==c can be true.
Order matters.
>> String a = "foo";
A string "foo" in the pool is created.
>> String b = "food".substring(0, 3);
Another string "foo" in the pool is created.
>> String c = b.intern();
The pool is being searched for a string "foo". One is found. The same as variable 'a' is refering to.
Hence a==c. They're equal since they both "point" to the same pool string containing "foo".
>> String a = "foo";
A string "foo" in the pool is created.
>> String b = "food".substring(0, 3);
Another string "foo" in the pool is created.
>> String c = b.intern();
The pool is being searched for a string "foo". One is found. The same as variable 'a' is refering to.
Hence a==c. They're equal since they both "point" to the same pool string containing "foo".
ASKER
Thanks for adding more explanation. So does this means that String of pools contains a["foo"] and b["foo} in the order they are added. so when b.intern() is called "foo" is searched and a["foo"] is returned so a==c becomes true?
>> so when b.intern() is called "foo" is searched and a["foo"] is returned so a==c becomes true?
That's right.
That's why if b would have been defined as:
That's right.
That's why if b would have been defined as:
String b = "food".substring(0, 3).intern();
then b==c would be true.
ASKER
Thanks this clarifies my understanding!
You're welcome
Thanx 4 axxepting
Thanx 4 axxepting