aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Literals" Watch "String Literals" New topic
Author

String Literals

John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
Hi,
Could anyone please explain why the first statement is true and rest are false.
"String" == "String".trim() // is true
"String" == "String ".trim() // is false
"String " == "String ".trim() // is also false
"String ".trim() == "String ".trim() //also false
Thanks a lot. Cheers!~
Sumit


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Because any mutator method of class String (e.g. trim()) that changes the character pattern of a string, returns a new reference to a new string literal. Thus, the == comparison yields false. But, if the mutator method has no effect on the string literal, the same reference is returned.
"String" == "String".trim() // is true
trim() has no effect on "String", thus, the same reference is returned and the comparison yields true.
"String" == "String ".trim() // is false
"String " == "String ".trim() // is also false
"String ".trim() == "String ".trim() //also false
trim() has the effect of removing the last space character and a new reference to a new "String" literal is returned, thus the == comparison yields false.
Bottom line, == compares the references and not the content of a string. Use equals() to compare the content.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Good Job Val
Simply Suberb
good answer


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Whenever in doubt, it helps to check the source code of some J2SE classes. For instance, if we have a quick look at the String.trim() method:

On the last line, we can see that either "this" is returned when no change occured or substring is invoked if some change is required. Looking at substring, we can see that it "Returns a new string that is a substring of this string" (from the API), thus the returned string is a new reference...
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
String variables are such a trap for even experienced java programmers. normally any method that acts on a string will return a new reference - I was totally unaware that some returned the "this" pointer. is there a list of which functions do this? (i'm assuming any method of String that returns a string... but who knows?) this would be very handy to know.


use the [CODE] tags - it makes it much easier for people to help you.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Check out the source of the String class.
Basically, if a method does not mutate the String object, the same reference (i.e. this) is returned. For instance, the toString() method returns this. But, the substring() method could also return this only if the returned substring is the same as the one on which the method is invoked:
String s = "String";
String t = s.substring(0);
System.out.println(s==t); // prints true !!!
[ July 23, 2002: Message edited by: Valentin Crettaz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Literals