Solved

java string equality

Posted on 2013-02-01
9
1,026 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 500 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
tomcat startup error 5 101
how to install java on RHEL image on EC2 4 39
Is there a simpler dropbox system? 10 34
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

791 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