Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes trim() method in String 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 "trim() method in String" Watch "trim() method in String" New topic
Author

trim() method in String

Yea Mua
Greenhorn

Joined: Jul 02, 2011
Posts: 15
Hi all,

Here is a tricky question from abhilash's quiz:

" String ".trim() == "String" returns false
"String".trim() == "String" returns true
" String ".trim() == " String ".trim() returns false
"String".trim() == "String".trim() returns true

One more Q:

"String".toUpperCase() == "STRING" returns false

Any idea on what happened behind the scene?

Thanks!




OCPJP 6.0, OCEJWCD 6.0
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

that is because " String ".trim() returns new String("String"); and "String".trim() returns *this* object;i.e, the same "String".
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Added with Seetharaman,

By performing the operations like trim,toUpperCase,toLowerCase etc,change the state of the String,it returns the new String object.Otherwise it returs the same string object like this.So it returns true.
Ex:


Hope this helps.

Cheers Munees
My Blog
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

how about substring() method , does it return the same String object if we specify begin and end index such that it return whole string ?


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Javin Paul wrote:how about substring() method , does it return the same String object if we specify begin and end index such that it return whole string ?

Yes, it returns the same this string! why cant you test this?
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

just thought to put you in front of guru and benefit to others, not had my IDE opened thanks anyway
Yea Mua
Greenhorn

Joined: Jul 02, 2011
Posts: 15
Hi Muneeswaran and Seetharaman,

Thanks for the response, and I believe you guys must work for the exam team

Regarding this problem, I have a follow-up question upon your answers.

In my opinion, Java utilizes a String pool that virtually only one instance of String could exist. Strings with same value just means their reference all point to the only one instance. So how could a new String (after state change by applying trim,toUpperCase,toLowerCase etc) be created provided one has already lived there?

Thanks!


Muneeswaran Balasubramanian wrote:Added with Seetharaman,

By performing the operations like trim,toUpperCase,toLowerCase etc,change the state of the String,it returns the new String object.Otherwise it returs the same string object like this.So it returns true.
Ex:


Hope this helps.
Yea Mua
Greenhorn

Joined: Jul 02, 2011
Posts: 15
Call for a complete answer guys ~~
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trim() method in String