• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

question on "immutable" strings

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q for the ranch hands out there...
I bought the new Java 2 study guide (by Heller, Roberts), and there's a test question provided that doesn't seem right:
What does the following code print out?
1. class Cases {
2. public static void main(String[] args) {
3. String s = "abcde";
4. s = s.toUpperCase();
5. System.out.println(s);
6. }
7. }
a) abcde
b) ABCDE
Now, being that everyone knows that the original string IS NOT changed, I figured 'a' would be the correct answer- s is not modified, but a new string is created. However, it says 'b'....
The call on line 4 does not modify the original string; it creates a new string, and modifies s so that it points to the new string.
Why would reference s point to anything but the original string? This doesn't make sense....
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
there is a difference between a reference variable and an object variable just points to the actual object.
when they say strings are immutable they say the String object is immutable.
's' in the question is the reference it can be changed but the object it is pointing to is immutable.
if u dont want the reference not to be changed then u can make it final variable
if u make a variable final then u cant change the reference in the variable but can change the object itself. please note that u still wont be able to change an object if it is immutable like Strings and wrapper class objects
Cherry
[This message has been edited by Cherry Mathew (edited January 16, 2001).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tod
Its like this -
s points to the string object with "abcde".
When you call s.toUpperCase it returns a string object with "ABCDE" as it content.
s = s.toUpperCase reassigns s (so to speak) and s now refers (or points) to the second string object. This a new String object and its not that the old one is changed. So strings are immutable as you rightly pointed out.
Hope this makes sense. Others please correct me if I wrong.
 
tod murphey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope one of ranch hands is monitoring this, since it was almost resolved...
In looking through the Java 2 book of heller, roberts again, I found a question whose answer contradicts that of the first question regarding strings...
What happens when you attempt to compile and execute the following application?
1. class XXX {
2. public static void main(String[] args) {
3. String s1 = "abcde";
4. String s2 = "abcde";
5. s1.toUpperCase();
6. if (s1 == s2)
7. System.out.println("YES");
8. else
9. System.out.println("NO");
10. }
11. }
Now based on the first question, since the reference s1 will now be reassigned to the newly created string of ABCDE (since the original abcde was not modified), the answer should be NO, since
s1 = ABCDE, s2 = abcde. However, the answer is YES...
Now, what is going on here? What should be a simple rule of:
string- immutable
stringbuffer- immmutable
is turning out to be confusing, given these questions...
Please advise...
Thanks!
tem
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all StringBuffer's are mutable, contrary to what you wrote. Secondly in your first example it said s=s.toUpperCase(). In this statement you assigned the value of s.toUpperCase to s, thus changing the reference of s.
In your newest example, you never change the assignment, so when we get to the if statement, the program see's that s is still referring to the original String.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tod,
Sean is correct. If you re-write the code in line 5 of the second example to read:

You'll get a 'NO' result.
Strings can seem very confusing at first. There are a few points you have to keep in mind:

  1. Strings are Objects; the variable holds a memory address for the String Object, NOT the string itself
  2. Java uses a String Pool (a special area in memory) to store String objects created as literals or as the result of String constant expressions ie
    <pre>
    String s1 = "Hello"; // String literal
    String s2 = "Universe"; // String literal
    String s3 = "Hello" + "Universe"; // String constant expression
    </pre>
  3. String constant expressions create new strings which are stored in the String pool. In the above example 's3' points to a new string "Hello Universe" stored in the String pool; not the individual strings created by s1 and s2
  4. Strings references created using new or whose value is computed at runtime are always new String Objects; Java does not search the String pool for a matching String object
  5. String methods which change an existing String object always return a new String object. If no change occurs as a result of the method then the original reference is returned

  6. For example,

    Hope this helps.
    ------------------
    Jane Griscti
    Sun Certified Java 2 Programmer
    "When ideas fail, words come in very handy" -- Goethe
    [This message has been edited by Jane Griscti (edited January 18, 2001).]
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at 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