• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

trim() method

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 [email protected]
i've lost your mail address.

raghav..
 
Raghvendra Sharma
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ajith. Your explanation is very clear.
Marilyn
 
Raghvendra Sharma
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Ajith, thanks a lot.

raghav..
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic