Why string is immutable or final in java?

By Preeti Jain Last updated : January 31, 2024

Strings is a sequence of characters. In this tutorial, we will learn whether Java string is immutable or not, and the reasons behind it.

First of all, you should know what is mutable and immutable objects in Java.

Mutable objects in Java

The mutable objects are the Java objects whose states can be changed after their creation. That means you can change the values of their fields; you can add and remove elements.

Example

The below example demonstrates the mutability of an object (StringBuffer Object).

class Main {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Preeti");
    sb.append("Jain");
    System.out.println(sb);
  }
}

Output

PreetiJain

In the above example, only one object is created and whenever we are performing any changes in an existing object then changes will be reflected. It means we will get results like "PreetiJain".

Immutable objects in Java

The immutable objects are the Java objects whose states cannot be changed after their creation. That means you cannot change the values of their fields; you cannot add and remove elements.

Example

The below example demonstrates the immutability of an object (String Object).

class Main {
  public static void main(String[] args) {
    String s = new String("Preeti");
    s.concat("Jain");
    System.out.println(s);
  }
}

Output

Preeti

In the above example, only one object is created and that is pointed to an original string and whenever we are performing any changes in an existing object then changes will not get reflected. It means we will get result "Preeti".

Java string is mutable or immutable?

In Java, the string is immutable which means you cannot modify the states of a string object.

Why string is immutable or final in Java?

In Java, the string is immutable because of the following reasons:

  • To keep the sensitive information secure.
  • To maintain thread safety when you are working with multithreading concepts.
  • To cache and reuse a string.
  • To achieve hashing, to keep hash code (keys) unchangeable throughout the whole program.

In the case of String, several references can point to the same object. If any changes are performed by any reference then other references will be impacted. Thus, String is immutable in nature.

Example demonstrating Java string immutability

public class Main {
  public static void main(String[] args) {
    // Creatint a string object
    String str = "IncludeHelp";

    // Creating a new string object by adding string 
    // to original string (str)
    String updatedStr = str + ".com";

    // Modifying original string
    str = str.concat("Okay");

    // Printing strings
    System.out.println("Original String: " + str);
    System.out.println("Updated String: " + updatedStr);

    // Demonstrating that the original string is not modified
    System.out.println("After making changes the original String is: " + str);
  }
}

Output

Original String: IncludeHelpOkay
Updated String: IncludeHelp.com
After making changes the original String is: IncludeHelpOkay

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.