• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strings

 
poornima viswa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the following codes:
if ("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

The above code complies and prints "Equal"
Now consider the code below:
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
This code complies and prints Not Equal.
My doubt is i thought it would print Equal because since "String" is already present in the pool,the complier does not create a new copy but refers to the existing one in the pool
Why is the above program printing "Not Equal"
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the trim function does something, it will create a NEW string. Strings are immutable, so if you change it, it is not really changing the string, but creating a new string. Since two string objects will not be == eventhough equals() will be true, you get the NOT EQUAL answer.
Try taking the spaces out before and after " String " and you should get EQUALS.
Bill
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Poornima,
Bill is right...("Hopw can i ever Doubt him")

In the first question you have same string object and so it belong to the pool. When you write "String".toString() ... you are explicitely calling toString() method. Where as on the right hand side operand "String" ... toString() method is implicitly called by the compiler. So both the string object are same and hence have same references.
In the second case when you write " String ".trim().... you are creating an string object which is different of right hand operand of == operator. (i.e. "String"). As Bill already told you this is happening because String are immutable.

In addition to String class all the wrapper classes are also immutable. That is Byte, Short, Integer, Long, Float, Double, Character and Boolean.
Since String are immutable ...java have provided peer class of String known as StringBuffer which is not immutable.
Bill had already give the correct answer....I just elaborated it and added one more point. Hope it is clear to you. Also where ever you are satisfied with the answer then pls do mention it so that this thread could be closed by baretenders and thus save space...and things should be more clear of other visitor ...visiting this thread.

Regards,
Raj.
 
vishad patel
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,how are?
Thanks for sending good question.
Bye.
see you.
 
poornima viswa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx bill i got the answer. I am posting the following which i had a doubt before but now i am clear about the answer
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
output Not Equal
if("String".replace('T','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
output is Equal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic