String class is immutable in java. If you take dictionary meaning of immutable, it means unable to be changed or unchanging over time, so String is unchangeable or unmodifiable in java.
Let's understand with example.
String str1="Hello"; str1.concat("java2blog"); System.out.println(str1); // Output will be HelloAs you can see, value of str1 did not change. It created another String object with value "Hellojava2blog" but did not change String str1
Now let's understand what are potential reasons to make string immutable in java
String pool :If you simply assign a Value to String using double quotes, it is stored in area called string literal pool and one string can be referenced by many reference variables and if String Is mutable, then it will affect all reference variables.
Thread safe :Immutable objects are by default thread safe, so you don't need to put synchronisation for it and String instance can be safely shared among multiple threads.
Security :If String is not immutable then it may cause multiple security issue.
For example, while connecting to database, you provide username, password, Port and host name etc as String, if String is mutable, any hacker can change the reference value and cause security threats to application.