Results 1 to 20 of 20
  1. #1
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Strings and Immutable

    Okay so I heard that java Strings are Immutable, Constants. fair enough, but that means they cannot change right?

    So how come I can do this:
    Java Code:
    String mystr="me";
    mystr="meplusonethousand";
    has mystr not being changed?? if I ran a println on mystr i know i would get the meplusonethousand output??? why is this? or do I just not get immutable lol

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You're confusing the object with the variable. Variables, such as mystr, can refer to different objects, but the String object itself is immutable. This is why String methods such as replaceAll don't change the underlying object that calls the method, but rather must return a new String with the changes.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    but I thought that Strings were objects, that is what I find so confusing about the whole thing, they are not simple data types in java.

    Are Strings ultimately pointers that just change addresses and the methods they invoke behind the scences just accomplish this or have I it all wrong again???

    What is the difference between a String, and a StringBuilder I think would clarify this for me.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    but I thought that Strings were objects, that is what I find so confusing about the whole thing, they are not simple data types in java.
    They are objects, and String variables are reference variables.

    Are Strings ultimately pointers that just change addresses and the methods they invoke behind the scences just accomplish this or have I it all wrong again???
    Again, you're confusing String objects with the variables. The variables are reference variables which can refer to a String object, can refer to null, and can have the object they refer to changed by the assignment operator. In this they are no different from other reference variables.

    What is the difference between a String, and a StringBuilder I think would clarify this for me.
    A StringBuilder is a completely different object that allows you to create Strings, that is mutable.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Okay thanks for the information so far curmudgeon, I think I have a grasp what you are saying but one thing still baffles me, is the JPassword field, it comes as a char array, presumably because char arrays don't wander about in memory, but how come if a String can be set to null is it necessary to have a JPassword as a char array???

    Is this not the same as changing the contents of the String variable???

    e.g String password=new String(whateverCharArray);

    password=null;

    where is the seciurity risk after this?

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I'm not sure of the answer to this. I do know that many String objects are held in the String pool so that they can be re-used, and they may remain in this pool whether or not the variable that references them is set to null, so perhaps this has something to do with this question, but I can't say for sure.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Variables, such as mystr, can refer to different objects, but the String object itself is immutable. This is why String methods such as replaceAll don't change the underlying object that calls the method, but rather must return a new String with the changes.

    Okay after rereading this explanation I do understand the String class, however I do not obviously understand memory if a String variable is assigned null after being previously a confidential password and a char array which was a JPasswordField becomes Arrays.fill(""); etc. that the JPasswordField Array of chars is fundamentaly safer than a String variable??

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Okay after rereading this explanation I do understand the String class, however I do not obviously understand memory if a String variable is assigned null after being previously a confidential password and a char array which was a JPasswordField becomes Arrays.fill(""); etc. that the JPasswordField Array of chars is fundamentaly safer than a String variable??

    If a String variable holds a password and is then set to null, the String object itself still exists until it is collected by the garbage collector, something which may take a long time and which you have little control over. So using a mutable object such as a char array that can be cleared is safer.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    the String object still holds its previous assignment of the variable even though the variable has changed its contents???

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    the String object still holds its previous assignment of the variable even though the variable has changed its contents???
    Yes, absolutely. This is what we've been discussing this whole time -- Strings are immutable. Again, the variable may be made to refer to a different String object or null, but the original String object itself is immutable and does not change until it is destroyed by GC.

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I suggest that you sleep on this. I think that it will all be more clear to you if you give your brain time to mull it over. At least that's how my brain works.
    Last edited by curmudgeon; 06-11-2010 at 03:43 AM.

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by curmudgeon View Post
    Yes, absolutely. This is what we've been discussing this whole time -- Strings are immutable. Again, the variable may be made to refer to a different String object or null, but the original String object itself is immutable and does not change until it is destroyed by GC.
    Jesus was it that hard to implement a safe String? Thanks for all the replies curmudgeon. I learned a little bit more today which is always my goal :)

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Jesus was it that hard to implement a safe String?
    You'll have to ask the Java gods this question. I've no idea why things are made the way their made, but so far it seems to work pretty well for me.

    Thanks for all the replies curmudgeon. I learned a little bit more today which is always my goal :)
    You're quite welcome. You seem to be asking the right questions.

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Just one last question which has been annoying me this whole thread, why are other instances of a class called objects i.e MyClass obj = new MyClass()

    and Strings (assuming they are instances which maybe they are not, since you call them variables like an int or a double) are declared like String mystr; which would seem to me reading would be like "mystr is an instance of the class String"

    is the implementation just different for Strings?

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    No, your understanding is wrong. String is a class like any other. A variable that refers to a String object is a reference variable, just like any variable that refers to any object.

    The only special treatment given to String is that the mathematical addition operators (+ and +=) can be used to concatenate Strings.

    db

    edit And that String literals are automatically interned in the String pool, and other Strings can be specifically interned by invoking intern()

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Isn't there some web site description somewhere that explains objects and references in terms of cups? That might help, if anyone has it handy.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    That's the one.

  19. #19
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Thanks for the links, I think I understand it a bit more I'm just not 100% on the differences between how class instances and primitives types are treated by memory.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Thanks for the links, I think I understand it a bit more I'm just not 100% on the differences between how class instances and primitives types are treated by memory.
    Objects live in a heap, in one of the separate spaces managed by the garbage collector. Primitives can be members of objects so they can also leave in the heap; primitives can also be local variables and are stored on the stack and finally, static final primitives can even be part of the (virtual) machine code.

    kind regards,

    Jos

    ps. this was an entirely different question so it belonged in its own thread.

Similar Threads

  1. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  2. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 12:58 PM
  3. Passing Strings ?
    By aldo1987 in forum New To Java
    Replies: 17
    Last Post: 04-24-2009, 05:03 AM
  4. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 03:08 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
  •