Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

String question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("string" == "string"); //output: true
System.out.println("string".toString() == "string"); //output: true
System.out.println(" string ".trim() == "string"); //output: false
So " string ".trim() returns a new String instance of "string" which is not the literal String instance "string"?
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
" string ".trim() constructs and returns a new string so the reference is not the same anymore ie. its a new object.
You would get true in you used an equals() call instead in the 3rd line because its looking for the same value.

Bye,
Terry
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since String objects are immutable, every method that tries to manipulate them will return a new String object.
The implementation of the toString() method in the String class is a little special: it does not try to manipulate the String object, it just return the same object.
FYI, the implementation is :

 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
Another special case of String class as far as I know:
System.out.println("STRING".toUpperCase() == "STRING");//true
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The trim() method returns the reference to the same object if it does not need to be modified, that is it has no leading and trailing blanks. If the original String object needs to be changed then a reference to a new String object is returned.
Hope this helps.
Vani
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can find similar questions about string in
http://www.angelfire.com/or/abhilash/Main.html
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to try this
http://www.tipsmart.com/studytools/selfreview/objequiv.htm
-Sandeep
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!