Immutable String in Java

In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.

Once string object is created its data or state can’t be changed but a new string object is created.

Let’s try to understand the immutability concept by the example given below:

Why string objects are immutable in java?
Because java uses the concept of string literal. Suppose there are 5 reference variables, all refer to one object “Sachin”.If one reference variable changes the value of the object, it will be affected by all the reference variables. That is why string objects are immutable in java.

avatar

Thank you for all your help and assistance over the years with our products.

avatar

"My experience at Java E Training is valued and I loved every leaning moment here."

Sai
Phone: +91-9640400300
Email: mahesh@javaetraining.com,
Email: javaetraining@gmail.com
Gachibowli,
Hyderabad, IN 500032