Results 1 to 5 of 5
  1. #1
    anjanesh is offline Member
    Join Date
    May 2007
    Location
    Mumbai, India
    Posts
    5
    Rep Power
    0

    Default Strings are immutable yet they can be changed ?

    Hi

    Trying to get the hang of Java.

    In the String doc, its mentioned :
    Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings.
    But in that case how come this works ?

    PHP Code:
    String s = "abcde"; System.out.println(s);
    s = "12345"; System.out.println(s);
    Thanks

  2. #2
    fritz is offline Member
    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Default

    because you are not changing the String, you are changing what the 's' reference points to.

    in other words, 's' is NOT a String, it is a REFERENCE TO a String. 's' lives on the stack, and the String object containing "abcde" lives in the heap (let's not get into the String pool right now). when you run the line

    s= "12345", a new String is created in the heap. 's' is changed to point to it, but the String "abcde" is still hanging around, unchanged.

  3. #3
    anjanesh is offline Member
    Join Date
    May 2007
    Location
    Mumbai, India
    Posts
    5
    Rep Power
    0

    Default

    So when s references to 12345, does the string obj containing abcde get deleted from the heap ?

  4. #4
    fritz is offline Member
    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Default

    no.

    String literals are a special case. whenever you have a double quoted string, it gets created in the string pool. these are not eligible for normal garbage collection.

    the details are a little more than you need to know right now, but basically:

    Strings are never changed. new strings can be created, and references re-assigned to them.

  5. #5
    anjanesh is offline Member
    Join Date
    May 2007
    Location
    Mumbai, India
    Posts
    5
    Rep Power
    0

    Default

    So is this a bad way to go about ?
    PHP Code:
    String s = "abcde"; 
    s = "12345"; 
    .
    .
    .
    s = "somethingelse";
    Is there an more efficient way (in terms of memory) to code this ?

Similar Threads

  1. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 10:10 PM
  2. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 03:46 AM
  3. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 04:39 AM
  4. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 06:56 AM
  5. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •