This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== doubt" Watch "== doubt" New topic
Author

== doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
How come the

if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Result is equal.

if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");


Result is not Equal.I don�t get it.Please help me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The "==" operator tells you if two objects are the same physical object -- i.e., the same spot in the computer's memory. Two Strings can contain the same exact characters, in the same order, and yet not be ==, because they're two separate objects. OK?

The String.toString() method returns the object you call it on, so x.toString() == x for any String x. The "trim()" method, on the other hand, may return a different String object, if the computed result is different from the original as here. So in your second test, you're comparing two different Strings, both with the same characters.

The "equals()" method would return true in both cases.


[Jess in Action][AskingGoodQuestions]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


1. "String".toString() == "String"-->> Calling toString() method on String object will return the same String object. So comparing it with same object return true.

The implementation of toString() in String class is something like this...




2. " String ".trim() == "String"--->>>> When there is any type of trimming opertaion like in "String " or " String" or in " String ", trim() method always return a new String object created using new. If there is no triming operation, then trim() method return same object. In you case since trim() method returns a new object, comparing it with String literal object which was referenced from String literal pool will return false so in this case the else part gets executed.

You can get more information on how String objects are created at here...

Strings, Literally by Corey McGlone


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thankyou very much.I got it.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi again,

The above one,
" String ".trim()=="String" returns false.Becuase both Strings are not in the same stringpool.Like this

I thought
"String".repace('g','g')=="String" ---->After replace the origianl g with another g.That string is in differnent place and the RHS String is in diffenrent place.so both referes to the different object and thought it would retuurn false.

But "String".repace('g','g')=="String" returns true.How come Both String are in the same stringpool.In other words,both references refer to the same object?
Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389
If u look at the source code of String.java, you will find that if the two chars are one and the same, it returns the same object.

the source is something like this,



Hope this helps!
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Originally posted by Shiva Mohan:
Hi again,

The above one,
" String ".trim()=="String" returns false.Becuase both Strings are not in the same stringpool.Like this

I thought
"String".repace('g','g')=="String" ---->After replace the origianl g with another g.That string is in differnent place and the RHS String is in diffenrent place.so both referes to the different object and thought it would retuurn false.

But "String".repace('g','g')=="String" returns true.How come Both String are in the same stringpool.In other words,both references refer to the same object?


This is from JavaDoc.

replace
public String replace(CharSequence target,
CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.


public interface CharSequence

This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other


SCJP 1.4 85%
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

But "String".repace('g','g')=="String" returns true.How come Both String are in the same stringpool.In other words,both references refer to the same object?


As I posted earlier about trim() that the trim() method will return you a new object if trimming occurred. i.e., if there is certain change in the content of the string.

Similary with replace(), a new object gets created if you really changed the content of earlier string.

But in "String".replace('g','g')--->>> you content is still same. still its "String". Had it been "String".replace('g', 'G')--->>> then you will get a new object

"String".replace('g', 'G')=="String" ---->>>>>> return false


Naseem
[ July 20, 2006: Message edited by: Naseem Khan ]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks again Naseem.I am looking forward for your answers as it is very clear and well explained.Thankyou very much.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hello again & again ,
This == questions still gives trouble for me to get.How come this
0.0==-0.0 return true.Please help me.
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
This is an exception, it only works with 0. I believe, it was done to remove any ambiguity regarding 0 being positive or negative.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
15.21.1 Numerical Equality Operators == and !=
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == doubt