This is no doubt, most asked beginner level java
interview question. And sometimes you can face at medium level interviews also.
So, my suggestion is to learn it here and for ever.
Java String is a immutable object. For an immutable object you cannot modify any of its attributeís values. Once you have created a java String object it cannot be modified to some other object or a different String. A reference to a java String instance is mutable. There are multiple ways to make an object immutable. Simple and straight forward way is to make all the attributes of that class as final. Java String has all attributes marked as final except hash field.
We all know java String is immutable but do we know why java String is immutable? Main reason behind it is for better performance. Creating a copy of existing java String is easier as there is no need to create a new instance but can be easily created by pointing to already existing String. This saves valuable primary memory.first we understand what is immutable?.
What is immutable?
b) never changing.
c)Immutable object is an object whose state
cannot be modified after it is created
Normally immutability in java is achieved through
1)Donít provide mutator methods for any field
2)Make all fields final and private
3)Donít allow subclasses by declaring the class final itself
4)Return deep cloned objects with copied content for all mutable fields in class
Please note that while it is possible to implement immutability without "final" keyword, its use
makes that purpose explicit, to the human (the software developer) and the machine (the compiler).
Java also has its share of immutable classes which are
primarily String class and wrapper classes. In this post, we will
understand the need of immutability for String class.
1) Security : The first and undeniably most important reason is security. Well, its not only about your application, but even for JDK itself. Java class loading mechanism works on class names passed as parameters, then these classes are searched in class path. Imagine for a minute, Strings were mutable, then anybody could have injected its own class-loading mechanism with very little effort and destroyed or hacked in any application in a minute.
[ Well, I think in this case java didn't have got any popularity today... :-) and nobody would be using it]. It means Strings were immutable thatís why java is still around in the game.
2) Performance : I believe that it is not the immutability of String class that gives it performance, rather it is string pool which works silently behind the scene. But at the same time, string pool is not a possibility without making String class immutable. So, it all again comes down to immutability of String class which allowed string pools, and thus better performance.
3) Thread safety: Immutable objects are safe when shared between multiple threads in multi-threaded applications. Just understand and learn it. There is no super logic. If something canít be changed, then even thread can not change it.
4)class final:Donít allow subclasses by declaring the class final itself.
|String name = "alok"; String fullName
= name + "sharma";
Here when we declared fullName you would expect it to be appended to the system memory location where the variable name is saved. But in Java it does not happen this way.
Here a new variable fullName would be created in memory and the value would be saved there.
This is termed as immutable. i.e., String objects cannot be modified in memory once they are created. You may be able to view the change in your program but new String objects would be created in the system memory.