Solved

java string equality

Posted on 2013-02-01
9
1,017 Views
1 Endorsement
Last Modified: 2013-02-01
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.
1
Comment
Question by:javaagile
  • 5
  • 4
9 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
We're not allowed/here to do your homework. Sorry.
0
 

Author Comment

by:javaagile
Comment Utility
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?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
Comment Utility
The answer is b.

The java docs of intern() say:
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

That's why a==c.
For a==b to be true, b should have been defined as:

String b = "food".substring(0, 3).intern();

Open in new window

0
 

Author Comment

by:javaagile
Comment Utility
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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".
0
 

Author Comment

by:javaagile
Comment Utility
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?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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:
String b = "food".substring(0, 3).intern();

Open in new window

then b==c would be true.
0
 

Author Closing Comment

by:javaagile
Comment Utility
Thanks this clarifies my understanding!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
You're welcome
Thanx 4 axxepting
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
mapShare challenge 13 66
micro services spring boot application error 3 23
Java Timer (static) 9 5
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now