A String in Java is immutable, which means once you create a String object, its value cannot be changed. Any changes (such as concatenation) made to the string will create a new String object. This immutable nature of String in Java provides several advantages such as security, thread safety, and performance.
Features of Immutable Strings in Java
There are several advantages of immutable string in Java, some of them are:
- Security: Immutability ensures that sensitive data stored as strings such as passwords, user personal details etc. cannot be changed once created. This makes data stored as strings more secure.
- Thread Safety: Immutability makes string thread-safe, which means multiple strings can access string objects at the same time. This improves performance.
- Caching and Memory Management: Java uses a string pool to manage string literals. A string literal is a string that is created without using new keyword, for example: String str = “hello”; If a string is created with a value that already exist in the pool, then JVM returns the reference of existing string. This reduces memory usage.
Examples of Immutability
In this example, we have a string str
, which contains the value “Hello”, when we concat a string to str
, it creates a new string object modifiedStr
with the value "Hello, World!"
.
public class ImmutableStringExample {
public static void main(String[] args) {
String str = "Hello";
String modifiedStr = str.concat(", World!");
System.out.println("Original String: " + str); // Output: Hello
System.out.println("Modified String: " + modifiedStr); // Output: Hello, World!
}
}
How Immutable Strings Work
- String Pool: Java maintains a string pool, when you create a new string, JVM checks in the string pool. If the identical string is found in the pool, JVM returns reference to it.
- String Methods: Java String Methods such as
substring()
,concat()
,replace()
, etc do not change the original string. Instead, they create and return a new string.
Example of String Pool
In this example, we create two strings s1 and s2 using string literals (without using new keyword). String s3 is created using new keyword. Hence s1 and s2 refers to the same string in the pool, however s3 refers to different object because it is created using new keyword.
public class StringPoolExample {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");
// true, both refer to the same string in the pool
System.out.println(s1 == s2);
// false, s3 refers to a new string object
System.out.println(s1 == s3);
}
}
Example of String Methods
In this example, when we used toUpperCase()
method on string str
, it didn’t change the value of str
, rather returned a new string object with modified value.
public class StringMethodsExample {
public static void main(String[] args) {
String str = "java";
String str2 = str.toUpperCase();
System.out.println(str); // Output: java
System.out.println(str2); // Output: JAVA
}
}
Leave a Reply