Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trim() method in String

 
Yea Mua
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is because " String ".trim() returns new String("String"); and "String".trim() returns *this* object;i.e, the same "String".
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about substring() method , does it return the same String object if we specify begin and end index such that it return whole string ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just thought to put you in front of guru and benefit to others, not had my IDE opened thanks anyway
 
Yea Mua
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call for a complete answer guys ~~
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic