aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Pool Question 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 "String Pool Question" Watch "String Pool Question" New topic
Author

String Pool Question

Akhil Maharaj
Ranch Hand

Joined: Sep 29, 2007
Posts: 63
Hi ,

When stringObject.toUppercase() is called
- Is the new String object gets generated on the heap / or pool ?

Can anybody explain the following code ?

Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Interesting question. The common implementation of String.toUpperCase() will actually return the original object if the object is already all uppercase, which is why (s == str) evaluates to true in your program. However, as far as I can tell, this is not a behavior guaranteed by the API, so this behavior can theoretically be different for other library implementations (someone please correct me if I'm incorrect).

The moral of the story? Use String.equals() exclusively whenever you want to test for string equality, and you won't get tripped up by these implementation-specific quirks.


SCJP 5.0
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 418
    
    2

s1 = s.toUpperCase ();

toUppercase returns the original string if there is nothing to do, I found this in some Sun documentation its not well documented though, i.e. I've seen comments in the online java docs that don't make this clear at all.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

Note: Please quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
abhishek pendkay
Ranch Hand

Joined: Jan 01, 2007
Posts: 184
but what happens if the original string is not in uppercase and a new string is created.. is it on the heap or the pool ???


The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 418
    
    2

For Jesper ;-) ...

this is definitely badly defined territory (or interesting depending on your point of view ) but ...

http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#toUpperCase(java.util.Locale)

"toUpperCase

public String toUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale, which is returned by Locale.getDefault.

If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned.

Otherwise, this method creates a new String object representing a character sequence identical in length to the character sequence represented by this String object and with every character equal to the result of applying the method Character.toUpperCase to the corresponding character of this String object. "

In answer to the new question the rule of thumb is if the string literal is known at compile time its pooled , at run time a new string is created and you have to call intern to pool it but see the above for one exception, to start with ;-)
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Chris Hurst:
If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned.

Interestingly, this paragraph is no longer present in the API documentation for Java 1.4 and 1.5--which suggests that this behavior is no longer guaranteed. So right now this is a implementation-specific optimization quirk rather than officially specified behavior.
dipayan chatterjee
Ranch Hand

Joined: Oct 03, 2007
Posts: 47
the stringobject.toUpperCase()will return the original string if all charecters are in uppercase but if all charecters are'nt capital case then as we know strings are immutable so a new string object is returned with all charecters in uppercase and as all objects are stored on the heap so the returned string object too will be placed on the heap


SCJP1.4 , gearing up for SCWCD 5
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
this is an interesting discussion, and the the good news is that it's way out of scope on the real exam - in other words, if you're focusing on studyng for the exam you can skip this discussion!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

Originally posted by Chris Hurst:
For Jesper ;-) ...

Thanks Chris, but this was meant for the original poster (Akhil Maharaj). If you copy a question from a book or mock exam, we want you to tell us where you copied it from. The quote your sources page explains why this is necessary.
Akhil Maharaj
Ranch Hand

Joined: Sep 29, 2007
Posts: 63
Jesper ,

While working on the questions given in one of the mock exams , I came across this . This problem may not be exactly same as the one , I have asked here . I have added some more complications into it .

Thanks to all those for jumping on complicated String issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Pool Question