[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java string equality

Posted on 2013-02-01
9
Medium Priority
?
1,062 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
ID: 38843276
We're not allowed/here to do your homework. Sorry.
0
 

Author Comment

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

Accepted Solution

by:
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

0
Independent Software Vendors: 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

by:javaagile
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.
0
 
LVL 37

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Closing Comment

by:javaagile
ID: 38843486
Thanks this clarifies my understanding!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38843510
You're welcome
Thanx 4 axxepting
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 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