• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

'==' operator on String objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Could anyone please explain the reason for these answers
if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer:
the code will compile an print "Equal".
--------------------------------------------------------------------------------
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Answer:
the code will compile an print "Not Equal".

--------------------------------------------------------------------------------
Thanks for any early response
SDave Bee..
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the first one fooled me, because I thought toString() might return a new String object, which evidently is wrong. Since String objects are immutable, it will just return the same object, as indictaed in the docs. My guess it's just more efficient design that way.
The second one is really asking whether the methods of String return a String object from the constant string pool, if possible. The answer must be no, and I have not read anything which suggests that is the case. The only String method I know of which does this is String.intern(). It will return the String object from the pool if it exists.
String s1 = "Java";
String s2 = new String("Java").intern();
s1 == s2 -> true.
 
SDave Bee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joseph ,Thanks for ur reply ,it's really helped a lot for me.
But i come accross another question which is:
Byte b1 = new Byte("127");

if(b1.toString() == b1.toString())
System.out.println("true");
else
System.out.println("false");
Answer: false

Does this because of the IMMUTABLE NATURE of the wrapper classes? or we can only change the wrapper objects with "parseInt(String)"and such kind of methods ?
Thanks in advance for reply
SDave
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Joseph:
Well, the first one fooled me, because I thought toString() might return a new String object, which evidently is wrong. Since String objects are immutable, it will just return the same object, as indictaed in the docs.


I think that in the first example, we have the case that the string literals are automatically intern()ed for you? Therefore the reference comparison is using the same values.
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by SDave Bee:
Byte b1 = new Byte("127");

if(b1.toString() == b1.toString())
System.out.println("true");
else
System.out.println("false");
Answer: false


I think that unlike your first case where we are dealing with string literals, the toString() on the byte object _has_ to make a new string for each call and therefore the references differ.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that the wrapper classes to the primitve number types are immutable as well and can't be changed. There is no method which will change the value of an Integer object, for example.
There are only two constructors for each of these (one takes a string, the other takes the primitive of the wrapper "type").
 
SDave Bee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,thanks for the reply.
SDave
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is actually very simple. Any time that a method that works on a String has to change the contents of the String, it returns a brand new String object created on the heap. Any time that method that works on a String does not have to change the contents of the String, it returns the very same String, not a new one.
So we get this:
" String ".trim() == "String" returns false
"String".trim() == "String" returns true
 
reply
    Bookmark Topic Watch Topic
  • New Topic