• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

== doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much.I got it.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
15.21.1 Numerical Equality Operators == and !=
 
reply
    Bookmark Topic Watch Topic
  • New Topic