This discussion is archived
1 2 3 6 Previous Next 76 Replies Latest reply: Nov 19, 2008 11:47 AM by 843785 RSS

Why String Objects are immutable ?

843785 Newbie
Currently Being Moderated
From the JLS->

A String object is immutable, that is, its contents never change, while an array of char has mutable elements. The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

Why are String objects immutable ?
  • 1. Re: Why String Objects are immutable ?
    3004 Newbie
    Currently Being Moderated
    My guess would be performance, memory, security, and design.

    String is basically a "value" class. That is, a String object is intended to represent a single, simple value. It's often good practice to make classes of this nature immutable--just like Integer, Double, etc. are.
  • 2. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    Immutable Classes Run faster ?
    How do you make a class immutable ?
  • 3. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    gtjdjtnd wrote:
    Immutable Classes Run faster ?
    Did anyone claim that? In any case, that's not a reason to make a class immutable .
    How do you make a class immutable ?
    Don't allow any methods to change the state of an object.
  • 4. Re: Why String Objects are immutable ?
    DrClap Expert
    Currently Being Moderated
    BigDaddyLoveHandles wrote:
    gtjdjtnd wrote:
    How do you make a class immutable ?
    Don't allow any methods to change the state of an object.
    Actually that makes the objects immutable, not the class. But I believe classes (i.e. java.lang.Class objects) are already immutable, are they not? Class is a final class and I don't see any methods which change its state.
  • 5. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    DrClap wrote:
    BigDaddyLoveHandles wrote:
    gtjdjtnd wrote:
    How do you make a class immutable ?
    Don't allow any methods to change the state of an object.
    Actually that makes the objects immutable, not the class. But I believe classes (i.e. java.lang.Class objects) are already immutable, are they not? Class is a final class and I don't see any methods which change its state.
    Picky, picky!
  • 6. Re: Why String Objects are immutable ?
    DrClap Expert
    Currently Being Moderated
    BigDaddyLoveHandles wrote:
    Picky, picky!
    It's the nature of the business. People should be able to tell classes from objects, variables from references, and so on, otherwise they won't get anywhere programming Java.
  • 7. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    So its like a constant and a variable ?

    String works like a constant ?
  • 8. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    Does Java have "constants"?
  • 9. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    final.
  • 10. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    gtjdjtnd wrote:
    final.
    final what?
  • 11. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    As I understand, safety was the chief factor in making String immutable. This is important for Java security, but also in general, having objects (or primitives) at are immutable makes programs easier to reason about.
    String str = ...
    byte ar = ...
    if (str.equals("hello")) {
     return true; //str is guarenteed to still be "hello" here
    }
    if (ar[0] == 0) {
     return ar[0] //not guarenteed to return 0!
    }
    And by the way, the conditions for immutability are not just that there are no methods that change the object, but also the class should be final. Without making it final, it can be extended and hacked.
  • 12. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    I find these answers quite satisfying ...

    Here's a concrete example: part of that safety is ensuring that an Applet can contact the server it was downloaded from (to download images, data files, etc) and not other machines (so that once you've downloaded it to your browser behind a firewall, it can't connect to your company's internal database server and suck out all your financial records.) Imagine that Strings are mutable. A rogue applet might ask for a connection to "evilserver.com", passing that server name in a String object. The JVM could check that this server name was OK, and get ready to connect to it. The applet, in another thread, could now change the contents of that String object to "databaseserver.yourcompany.com" at just the right moment; the JVM would then return a connection to the database!

    You can think of hundreds of scenarios just like that if Strings are mutable; if they're immutable, all the problems go away. Immutable Strings also result in a substantial performance improvement (no copying Strings, ever!) and memory savings (can reuse them whenever you want.)

    So immutable Strings are a good thing.

    -------------------

    The main reason why String made immutable was security. Look at this example: We have a file open method with login check. We pass a String to this method to process authentication which is necessary before the call will be passed to OS. If String was mutable it was possible somehow to modify its content after the authentication check before OS gets request from program then it is possible to request any file. So if you have a right to open text file in user directory but then on the fly when somehow you manage to change the file name you can request to open "passwd" file or any other. Then a file can be modified and it will be possible to login directly to OS.

    JVM internally maintains the "String Pool". To achive the memory efficiency, JVM will refer the String object from pool. It will not create the new String objects. So, whenever you create a new string literal, JVM will check in the pool whether it already exists or not. If already present in the pool, just give the reference to the same object or create the new object in the pool. There will be many references point to the same String objects, if someone changes the value, it will affect all the references. So, sun decided to make it immutable.
  • 13. Re: Why String Objects are immutable ?
    3004 Newbie
    Currently Being Moderated
    gtjdjtnd wrote:
    Immutable Classes Run faster ?
    Classes don't run, methods do. But no, immutable classes are not inherently faster, but there are some optimizations that can be done if a class is known to be immutable.
    How do you make a class immutable ?
    By never changing its state.
  • 14. Re: Why String Objects are immutable ?
    843785 Newbie
    Currently Being Moderated
    gtjdjtnd wrote:
    From the JLS->

    A String object is immutable, that is, its contents never change, while an array of char has mutable elements. The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

    Why are String objects immutable ?
    There's room in Java for two string abstractions. You've got String which is immutable, and StringBuilder/StringBuffer which is mutable.

    In principle it's a design decision when to use which.
1 2 3 6 Previous Next