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

sonali rao
Ranch Hand

Joined: Nov 03, 2003
Posts: 64
Please read the following 2 questions.
I agree with the Question1's answer. But i dont understand why is the answer for Question2 "Not Equal".

Question 1.
if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answer
the code will compile an print "Equal".

Question 2.

if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Answer
the code will compile an print "Not Equal".
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Sonali -
In Java, the == tests for whether two object references point to the same object, not whether their values are equal. For equality testing of objects, you must use their equals() method.
Think of it this way: the == operator tests if a value is the same. What does a reference have? The address of the object it's referencing. Therefore,

Here, isSame will be false, because the references, a and b, have different addresses because they point to different objects. Since an address is the value for a and b, their values are different, and the comparison returns false.
I hope that helps!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
O Joseph
Ranch Hand

Joined: Jun 27, 2003
Posts: 75
To quickly add to that;
remember that String objects are immutable therefore the trim() method does
not change the original object - " String ".
The implication of this is that " String " and "String" do not refer to the
same object hence result is fault.
//N.B. difference? = white spaces.
i hope moi explanation is good enough!
Cya


XX.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805

remember that String objects are immutable therefore the trim() method does
not change the original object - " String ".

String.trim() returns a copy of the original string with the leading and trailing spaces removed. Therefore, the test is not

but

Immutability has nothing to do with the result.
O Joseph
Ranch Hand

Joined: Jun 27, 2003
Posts: 75
I beg to disagree with u. "String ".trim() is not assigned to any new
reference. therefore the original object remains unchanged and so :
/*if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");*/
prints "Not Equal".
" String " != "String" according to "==".
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You don't have to assign it to a reference if you're using it in the same place you create it. By invoking String.trim() in the test, you are testing the object returned by the operation -- a copy of the original String.
Try this simple test if you don't believe me:

The vertical bars act as an indicator to show whether or not the spaces are indeed trimmed.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi All,
I am still confused...
If " String ".trim() returns -->"String"
then, the statement if ("String"=="String")
should return true..isnt it?
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
String API for trim() method says that trim()

...let k be the index of the first character in the string whose code is greater than '\u0020' [white space], and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
Returns:
A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space

"String"=="String" references comparison should always return true because they are compile time constants pointing to the same object on the heap.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Cathy --
Two String objects can contain the same characters, in the same order, but still be distinct objects. Just like two identical cans of soup. They're identical in every way, but they're physically separate, so they're !=. The == operator means "are the same object," not "are equivalent." The "equals" method means "are equivalent", so " String ".trim().equals("String") returns true, because, although the two String objects being compared are located in two different places in the computer's memory, they both contain the word "String".
OK?


[Jess in Action][AskingGoodQuestions]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805

If " String ".trim() returns -->"String"
then, the statement if ("String"=="String")
should return true..isnt it?

No, because even though their values are the same, they have different addresses because they're different objects. For objects, == tests whether the objects have the same address; equals() tests whether the values of the objects are meaningfully equivalent.
For primitives, because the value of the primitive is a value and not an address, == tests the value. There is no equals() for primitives.
Here's a hypothetical, general example:

a and b are String object references, which means they can hold the value for an address to an object of type String.
The value of a is 1234, which is the address of the first String object.
The value of b is 1244, which is the address of the next String object.
Now, if we compare whether a == b, we are comparing whether 1234 == 1244. They're not equal, so the test returns the result "false". We could then compare a.equals(b), which tests whether the values are equal. In this case, we would see "true", because both objects have the value "abc".
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
if("String"=="String") compares references of two String literal constants. I believe they point to the same object on the heap and therefore the result is true.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Well, we all know that Java maintain a string pool for the string literal...
but I do not think String.trim() did say that it will return result coming
of the string pool.
So assuming " String ".trim() == "String" is a little too much.
In most cases when we used direct string literal all over our code, it might
be easier for the compiler to do the optimization to refer to the same string.
I would guess in cases where you have method like String.trim() which happen
at runtime, the API coder probably figure its not worth effort to check the
string pool and return existing strign...so it just create a brand new String object.
You'll probably get better result with " String ".trim().intern() == "String"
Just my 2 cents..
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Thanks, Wirianto.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi All,
Thanks for your inputs. So, let me try to summarize the post.
" String ".trim() returns a new String instance (not a literal)
That is why the following statement returns false.
if (" String ".trim() == "String)
Thanks again.
O Joseph
Ranch Hand

Joined: Jun 27, 2003
Posts: 75
here's moi own conclusion: the instance methods of the string e.g. trim()
look like they would transform the object they are invoked upon but
actually alter the object and instead return new string objects.implication?
yes, the implication is that comparing "==" gives false.
finne.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literals