Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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