• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may also want to try this
http://www.tipsmart.com/studytools/selfreview/objequiv.htm
-Sandeep
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic