File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equality Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equality Operator "=="" Watch "Equality Operator "=="" New topic
Author

Equality Operator "=="

La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
I thought "==" operator checks to see if the objects being compared are the same object reference till I tried the foll:
Sting s1="Hello";
Sting s2="Hello";
I tested with System.out.println(s1==s2?"same":"no");
I expected the answer to be "no" but I got "same";
But when I modified the code as under
String s1="hello";
String s2="he";
Stirng s3=s2+"llo";
and tested s1==s3? I got the answer "no".
Am I missing out on some concept here?


La Vish
SCJP 1.4, President 60s Club
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
For efficiency, the compiler creates a single String object to represent each String literal. In your example, "Hello" is a string literal.
Sting s1="Hello";
Sting s2="Hello";
Since the compiler only creates ONE object that contains the character sequence "Hello", both s1 and s2 are assigned a reference to this same object. That's why s1 == s2 is true.
In your second case:
String s1="hello";
String s2="he";
Stirng s3=s2+"llo";
and tested s1==s3? I got the answer "no".
There are 3 String literals created ("hello", "he", and "llo".) s1 and s2 clearly will contain a reference to a different object. s3's value has to be determined at run time. At run time, s3 is assigned a reference to a NEW String object that gets created by concatenating s2 and the literal "llo". Remember that since Strings are immutable, you cannot actually change their character sequence. That's why concatenation results in a brand NEW String object being created, and its character sequence is "hello". But since it's a new String, it can't possibly be the same String object currently assigned to s1, can it?
They are different String objects that contain the same character sequence.


Rob
SCJP 1.4
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
1) Format a = x ? b : c ;
The ternary operator has left to right associativity and x should be a boolean expression. s2 is checked if it returns true or false, then based on that, the value "same" or "no" is assigned to s1.

2) String s1="hello";
String s2="he";
String s3=s2+"llo";
s1==s3?
All string operations (concat, trim, replace, substring etc) construct and return new strings. s3 is a new string and thus is not == to s1.
Clement


best regards,<br />Steven<br />SCJP, SCEA
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
Thanks,Rob.You are legend.
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Oops, I overlooked the "==" as "=" in ur first part of the question.
s1==s2?"same":"no"
Sorry, my previous answer is invalid.
Clement
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
Thanks to you too,Clement.I am getting faith in myself now.
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
By the way, the ternary operator has right to left operator..
Gosh...must be having not enough sleep...thousand appologies...
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
It is ok Clement, I value your comments. Btw, don't loose too much sleep over it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equality Operator "=="