• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trim() method in String

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call for a complete answer guys ~~
 
The only cure for that is hours of television radiation. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic