Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

trim() method in String

 
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!



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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".
 
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.
 
Ranch Hand
Posts: 300
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 Windows XP Java
  • 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: 300
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 ~~
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic