java string equality

Posted on 2013-02-01
Medium Priority
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) {
    } else if (a == c) {
    } else {
} else {

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.
Question by:javaagile
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 37

Expert Comment

ID: 38843276
We're not allowed/here to do your homework. Sorry.

Author Comment

ID: 38843293
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?
LVL 37

Accepted Solution

zzynx earned 2000 total points
ID: 38843304
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 38843379
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.
LVL 37

Expert Comment

ID: 38843428
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".

Author Comment

ID: 38843445
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?
LVL 37

Expert Comment

ID: 38843468
>> 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.

Author Closing Comment

ID: 38843486
Thanks this clarifies my understanding!
LVL 37

Expert Comment

ID: 38843510
You're welcome
Thanx 4 axxepting

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

764 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