Immutable String

Question: Why is Sting class immutable?
Answer: String class is immutable. What this means is that String class is final, its state cannot be changes and String class cannot be sub classed. The reasons are listed below:
  • In java strings are used every where. For example, user names, passwords, data base URL, network connections etc. Suppose strings are mutable and if the string goes into wrong hands (hackers) then they can change the strings and hack any system and get access to critical data. So for security reasons String classes are made immutable.
  • As discussed in the previous question, immutable objects are thread safe and solves some synchronization issues.
  • Java designers have come up with a separate memory for string known as "String Pool". String pool is a special storage space in heap. To support string pool facility, strings are immutable. To know more about string pool, please read the answer to question 23 of ... Core Java interview questions and answers

No comments:

Post a Comment