Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== doubt

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou very much.I got it.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic