Immutable String In Java
Chapter:
String Handling
Last Updated:
24-03-2017 04:34:37 UTC
Program:
/* ............... START ............... */
public class JavaImmutableString {
public static void main(String args[]) {
String str = "Java";
str.concat(" Scan");// concat() method appends the string at the end
System.out.println(str);// will print Sachin because strings are
// immutable objects
}
}
/* ............... END ............... */
Output
Notes:
-
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.
- There are main three reasons behind for making String in java immutable are:
- 1. Security Issue
- 2. String Pool Issue
- 3. Thread Safe
- As String is an immutable object, it can be accessed by multiple threads and no thread can change the value of the particular object. So String objects are thread-safe and can be shared with multithreading environment.
Tags
Immutable String, Java, String