Why String is immutable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Why String is immutable?

    Can we have a discussion about it? ;)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What really do you want to discuss about it?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      What really do you want to discuss about it?
      Why String immutable ? i Just want to know the reason ?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        If they were mutable then there would be no String pool implementation and the performance gains of the String pool become lost.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Why String immutable ? i Just want to know the reason ?
          Suppose they were mutable; would you've asked the opposite question?

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            No actually it seems to be odd. String simply character array. If want to change it then we need to have it another variable?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              It actually seems rather odd to me that you knowing that String is just a character array would expect changing it to be easy when you know all the restrictions of arrays ...

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                .........! I forgot that.
                Then is the String is immutable for that reason?
                Last edited by r035198x; Mar 30 '09, 01:32 PM. Reason: removed a word

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  I did not say that is the reason. I was just using your words against you. I have already told you the performance reason in my first response.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Note that this 'space saving' trick can turn its head against you; have a look at the following code snippet:

                    Code:
                    String humongous= ... ; // an extremely big String
                    String small= humongous.substring(0, 1): // a very small String
                    humongous= null; // free the big one?
                    The substring() method shares its char[] buffer with the String for which it is a substring and the entire String buffer can not be garbage collected. Line two from the above code snippet had better written as:

                    Code:
                    String small= new String(humongous.substring(0, 1));
                    The String constructor guarantees that a new char[] buffer will be used.

                    kind regards,

                    Jos

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Originally posted by r035198x
                      I did not say that is the reason. I was just using your words against you. I have already told you the performance reason in my first response.
                      Actually sorry i didn't have a look at your first response.
                      @Josh
                      Thanks for your tricky code ;)

                      Comment

                      • askMeJFAQ
                        New Member
                        • Aug 2009
                        • 1

                        #12
                        I hope below comments make every one in commendable position to answer " Why String is immutable".

                        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.


                        Note: I do agree with "String pool implementation and the performance gains of the String pool" but it all w.r.t VM rather than OS.

                        Cheers
                        Bunty

                        Comment

                        • newbie325
                          New Member
                          • Feb 2012
                          • 1

                          #13
                          Originally posted by JosAH
                          Note that this 'space saving' trick can turn its head against you; have a look at the following code snippet:

                          Code:
                          String humongous= ... ; // an extremely big String
                          String small= humongous.substring(0, 1): // a very small String
                          humongous= null; // free the big one?
                          The substring() method shares its char[] buffer with the String for which it is a substring and the entire String buffer can not be garbage collected. Line two from the above code snippet had better written as:

                          Code:
                          String small= new String(humongous.substring(0, 1));
                          The String constructor guarantees that a new char[] buffer will be used.

                          kind regards,

                          Jos

                          Code:
                          String humongous= ... ; // an extremely big String
                          String small= humongous.substring(0, 1): // a very small String
                          humongous= null; // free the big one?
                          The substring() method shares its char[] buffer with the String for which it is a substring and the entire String buffer can not be garbage collected. Line two from the above code snippet had better written as:

                          Code:
                          String small= new String(humongous.substring(0, 1));
                          This is not right. Call to substring is going to call the constructor String(original String, startIndex, offset). So the substring is not sharing the char[] from original string

                          Comment

                          Working...