GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string 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 "string" Watch "string" New topic
Author

string

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi guys,
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
the ans is not equal.how?
thx


anushree
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The equality operator, ==, compares reference values. The String literal, "String", refers to an instance of a String in the String constant pool. The String created by the String.trim method is not a String constant. When the references of the two Strings are compared they are found to be not equal.
If the two strings were compared using the String.equal method then the two Strings would be found to be equal because they contain the same characters.
If you were to invoke the String.intern method on the String returned by the String.trim method then the result of the intern method would be a reference to the String in the String constant pool. If the equality operator were applied to the String literal and the result of the intern method then the result would be true.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
ok dan, but
if( "STRING".toUpperCase() == "STRING")
System.out.println("Equal");
else
System.out.println("Not Equal");
this results equal,
how?
ramki srini
Greenhorn

Joined: Aug 27, 2002
Posts: 26
toUpperCase() method will return a new String only if the value gets changed or else it will point to the same String in the pool.
In your case "STRING".toUpperCase will have no impact(as everything is uppercase already) on the "STRING" literal and so it will point to the same STRING in the pool and thats why the result EQUAL.
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
Hi all:
Let me do it.
if( "STRING".toUpperCase() == "STRING")
yes, it is true.
bucause
1. java put all String literals to a String pool. "STRING"s defined in two place are the same one in pool.
2.in LHS, toUpperCase() return the same reference since String "STRING" all uppercase, it neednt' change it and return a new one.


SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hey keen how about this one
if("String".substring(0) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
this results equal, explain me
thx
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
It's basically the same question as the previous one. The String methods first check to see if a modification of the existing String is necessary. If no modification is necessary then the method just returns the reference "this". For example, the substring method returns "this" when (beginIndex == 0) && (endIndex == count).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string