aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strings" Watch "strings" New topic
Author

strings

poornima viswa
Greenhorn

Joined: Nov 18, 2000
Posts: 16
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Aug 26, 2000
Posts: 126
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.


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
vishad patel
Greenhorn

Joined: Dec 05, 2000
Posts: 17
Hi ,how are?
Thanks for sending good question.
Bye.
see you.
poornima viswa
Greenhorn

Joined: Nov 18, 2000
Posts: 16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strings