This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How do the String methods work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How do the String methods work?" Watch "How do the String methods work?" New topic
Author

How do the String methods work?

Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

hi folks,
I came accross this question in a Mock test on www.javablackbelt.com


This, when run prints

false
true


I understand that String objects are immutable, so these when you apply these methods to a String object, a new String object is returned.
Above, both the substring() and trim() should return "String".
So, why do they give different results when compared to "String"?


Cheers,
Roy.
Sun Certified Java Programmer 1.5,
Sun Certified Web Component Developer 5.
"No pain. No Gain"
http://cs.nyu.edu/~rpm319
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41177
    
  45
Since Strings are immutable....performing trim() on " String " would not affect the original string(i.e. " String "). Therefore you are comparing " String " with "String"...which is obviously FALSE.

No. " String ".trim() is equal to "String", so the two strings have the same value. But the trim operation creation a new String object, and since "==" test for object equality -not string equality- the result is false.
[ May 13, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

but what about


Are'nt we getting a brand new String object from this as well?
Also, please look at the following cases:


[ May 13, 2008: Message edited by: Royston Monteiro ]
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I have written the below code and tried out, can anyone please help me out understand this ....



output :

String
false
true
STRING
false

Any one please when why this is happening like this..
[ May 13, 2008: Message edited by: Dinesh Tahiliani ]

Thanks<br />Dinesh
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
Go read this it might help you
The SCJP Tip Line Strings, Literally

* 'new' operator ALWAYS creates an object on heap.
* All methods in String class which return a string (toUpperCase(), toLowerCase(), trim() etc.) create the new object on heap, ONLY if required. If not required (say there are no white-spaces to trim), the same instance is returned.

regards
sachin verma
[ May 13, 2008: Message edited by: sachin verma ]

SCJP || SCWCD

Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
Hi,

Ideally trim() and substring() should have created new String objects. But substring() has an optimization with it.



As we can see in the code, "this" will be used when there is no need for a String to be created.

Similarly in trim(), "String".trim() == "String" would be true.
[ May 13, 2008: Message edited by: Vinayagar Karpagam ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do the String methods work?
 
Similar Threads
Strings
problem with equal and ==
confusion in String methods
Strings
Java Strings