aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Odd compares Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Odd compares" Watch "Odd compares" New topic

Odd compares

Fernando Pimentel

Joined: Sep 13, 2000
Posts: 6
I can not seem to remember which prep exam I found this in,
but here's a take on the question:
String s = "123";
String t = "123";
String u = new String ("123");
String v = new String (u);

Which of the following would be true:
1) s == t
2) s == u
3) s == v
4) u == v
5) s == "123"
6) s == new String ("123")
I believe that 1 & 2 are true.
I believe that 3 & 4 & 5 are false.
I thought that their answer said 5 was true also.
How can this be? Is it because Strings are immutable?
Is so, what about comparsions using wrapper classes?
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
i think the answers are 1 and 5 are true.
when using the "==", u are comparing references not content.
and yes string are immutable.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
1 and 5 are true. The only way the references are the same is if
1) you are STRICTLY using String Literals which always use the Constant Pool and therefore hold the same reference
2) If you are using String object created with the "new" operator and you specifically point 2 variables at the SAME object
1) s == t // both are literals so true
2) s == u // one literal one new object - false
3) s == v // one literal one new object - false
4) u == v // one literal one new object - false
5) s == "123" // both are literals so true
6) s == new String ("123") // one literal one new object - false
now if you say u = v; //both variables now point to same "new" object
u == v is now true.
[This message has been edited by Cindy Glass (edited March 21, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
Hi Cindy,
How can u say one is true.I think only 5 is true ..thats all.
We know == opt. checks for memory address...not the contents.
Correct me if I am wrong.
<marquee>Ratul Banerjee</marquee>
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
I don't understand the confusion.
If 2 literals of the same data type are being compared with the == operator the evaluation has to be TRUE if the 2 literals have the same value.
The == sign doesn't compare the memory address of the 2 literals. It compares the value of each literal to the other.
Athiveera Kugan

Joined: Mar 21, 2001
Posts: 15
In s=="123" statement,only '==' comparison operator is used not equals() method. equal operator compares only memory address of the s reference with corresponding value .
so that it returns false.


<I> s.a.kugan</I>
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi all..
well. Cindy is perfectly right when she says that 1) is true. let me explain..
As u amy know.. java maintains a string pool.. which contains references to all the string literals that are created by a program.. so if tehre is already a constant string existing in some code.. say "123" in this case and if a variable is again assigned the same value.. then the second variable points to the string already created in the string pool.. that is the saMe memory location as the first.. which means that the references are the same..
hope this helps..

Hima<BR>Sun Certified Java Programmer
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Cindy was right...,
A String Literal is implemented as an "anonymous String Object", only ONE anonymous String Object is SHARED by all String literals with the same contents.
for example :
s = "123";
t = "123";
both String references s & t denote the same anonymous String Object, initialized with the character string : "123"
so if we say :
s == t, or
s == "123", or
t == "123",
they are point to the same anonymous string object in which is shared by all string literals with the same contents("123"), hence the answer is always "true".

Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
Thanks Hima and Steve.
Your answers were much more complete than mine.
wood burning stoves
subject: Odd compares