jQuery in Action, 3rd edition
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

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How do the String methods work?" Watch "How do the String methods work?" New topic

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


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"?

Sun Certified Java Programmer 1.5,
Sun Certified Web Component Developer 5.
"No pain. No Gain"
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
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 ]
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 :


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.

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


Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72

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?
It's not a secret anymore!