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

trim() method

sanjay gautam
Ranch Hand

Joined: Aug 13, 2000
Posts: 35
can any one explain it :
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
why it will print "Not Equal".
thanks in advance
gautam
Jon Aryan
Greenhorn

Joined: Oct 06, 2000
Posts: 22
Hi gautam,
Hint:
* if there are some white spaces or control character in the String, then a substring operation is done on the String and a "new" String is returned after trimming the String.

* trim() returns the same String instance if nothing to trim !!!
-- try the ur code with an intern() method
like
if(" String ".trim().intern() == "String")
[ Later i will put some codes ]
sanjay gautam
Ranch Hand

Joined: Aug 13, 2000
Posts: 35
jon,
i didnot get your point. what i think is after trim() method a new string object will be created in string pool, which will be same to "String". so it should return true instead of false.
please make more clear.
gautam
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I think that " String ".trim() has the same address as the original string " String ". I think this because if you write your code using the String.equals( String ) method:

then your result is "Equal" because then you are checking whether the contents of the strings are identical. With == you are checking whether their addresses are identical.
Marilyn

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
If the <code>trim()</code> method has no effect on the string it is operating on, a new string will not be created, instead it returns the reference to the same string. Hence both == and <code>.equals()</code> returns true.
Consider for example the string "Hello World". Since this string has no leading or trailing spaces, calling the <code>.trim()</code> method on this string object has no effect on it because there is nothing to trim!!. In such a case, the <code>trim()</code> method will give you back the reference to the same object. Hence

- all the lines above will print true.
Note that it really doesnot matter if the string is created using the new() object or using the = operator. ie., whether the string is in the string pool OR whether it is on the heap. Since we are only talking about references here, it is sufficient to say the two references are equal.
On the other hand, if the string does have leading and/or trailing spaces, then the <code>trim()</code> method returns you a reference to a different object that has spaces trimmed off. Ofcourse in that case both reference comparison == and content comparison <code>.equals</code> returns false because we are comapring two different objects.
What's more..you can observe the same behaviour with the other 'pseudo-mutator' methods in the String class - <code>toLowerCase(), toUpperCase() , trim() </code>. All of them returns the reference to the string object on which the method is being invoked if the method has no effect on it. For example, calling <code>("HELLO WORLD").toUpper()</code> would return the reference to the same string because all the characters in the string in question are already in upper case!!
I will leave the rest to your imagination...... I would like to emphasize this is a really important concept for the sake of SCJP exam. Write a LOT of code to help you grasp the concept.
Hope that helps,
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Ajith,
If the trim() method has no effect on the string it is operating on, a new string will not be created, instead it returns the reference to the same string. Hence both == and .equals() returns true.

I understand this.
But IF the trim() method has an effect on the string, and therefore a new string is created, and IF the new string is identical to a string already in the string pool, why does the compiler not recognize that the new string (substring of the original string) is identical to the string already existing in the pool and assign the same address?
Marilyn
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
Hi sanjay,
Are you the sanjay gautam undergoing PSINET 6 months course in Hyderabad?
If yes, recognize me, I'm raghav, we had met there and i left the place due to some problems.
If you are the one, please mail me at s_raghu20@yahoo.com
i've lost your mail address.

raghav..
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
Dear sanjay,
The reason is simple, if the trim method has anything to do, a new string reference is created, otherwise the same string reference is returned.
In your case, the trim method has got to do something, and that's why == operator says not equal after comparing the references.
I hope the answer satifies you.
raghav..
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Good question Marilyn. Let's look at how the String.trim() method is written.

For now let's ignore the processing and look at the last line in blue color. The logic here is very simple. If there were any spaces in the original string, return the substring of the original string starting from the first non-white-space character('st') and ending at the last non-white-space character('len'). Otherwise, ie., if there were no trailing and leading spaces, return this reference which is nothing but the reference to the original object itself.
Since <code>trim()</code> returns the substring of the original string, the answer to your question is in the substring method. Here is the code for <code>String.substring(startindex,endIndex )</code> method -

Note that the string that is returned is not created in the string pool, but on the heap using the <code>new()</code> operator. This means even if such a substring already exists in the string pool it will be a different object than the string object returned by the trim function. Hence your reference comparison == returns false. By the way there are only two ways of explicitly creating a String in the string pool. One is by using a constant string literal and the = operator to instantiate a string and the otherone( less known?? ) is by using the <code>intern()</code> method.
You can further verify this argument by just using the substring method. Create two string objects, say 's1' and 's2', one in the pool and another one( 's2' ) using the new() operator such that the <code>s2.substring()</code> returns the value identical to the first string object 's1'. You will notice that the <code>s1 == s2.substring()</code> would return false indicating the substring method returned a totally new string object instead of reusing s1 from the string pool.
Hope that helps.
Ajith
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Thank you, Ajith. Your explanation is very clear.
Marilyn
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
Nice explanation Ajith, thanks a lot.

raghav..
 
GeeCON Prague 2014
 
subject: trim() method