Immutable String in Java

Immutable String in Java


Immutable String in Java

·??????A String is an unavoidable type of variable while writing any application program. String references

·??????are used to store various attributes like username, password, etc. 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.

?

?? Book Reference: Java The Important Reference: https://lnkd.in/g5HvrbXm

?

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

Testimmutablestring.java

class Testimmutablestring{

?public static void main(String args[]){

?String s="Sachin";

?s.concat(" Tendulkar");//concat() method appends the string at the end

?System.out.println(s);//will print Sachin because strings are immutable objects

}

}

Output:

Sachin

Now it can be understood by the diagram given below. Here Sachin is not changed but a new object

is created with Sachin Tendulkar. That is why String is known as immutable.

?

?

Why String objects are immutable in Java?

·??????As 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.

?

Following are some features of String which makes String objects immutable.

1. ClassLoader:

A ClassLoader in Java uses a String object as an argument. Consider, if the String object

is modifiable, the value might be changed and the class that is supposed to be loaded

might be different.

To avoid this kind of misinterpretation, String is immutable.

2. Thread Safe:

As the String object is immutable we don't have to take care of the synchronization

that is required while sharing an object across multiple threads.

3. Security:

As we have seen in class loading, immutable String objects avoid further errors by

loading the correct class. This leads to making the application program more secure.

Consider an example of banking software. The username and password cannot be

modified by any intruder because String objects are immutable. This can make the

application program more secure.

4. Heap Space:

The immutability of String helps to minimize the usage in the heap memory. When we

try to declare a new String object, the JVM checks whether the value already exists in

the String pool or not. If it exists, the same value is assigned to the new object. This

feature allows Java to use the heap space efficiently.

?

#Java #interview #questions #String #StringBuilder #StringBuffer #mutablestring #immutableString?#students #freshers #developers #job #placement #it #career?

Iftekhar Hossain

Software Developer | Java | Spring Boot | Spring Security | JPA | FinTech

1 年

I have published a long article about string immutability. It consists of lots of examples. You can check it from https://medium.com/@ifte.hsn/string-immutability-in-java-blessing-or-burden-c974b6cc0be7.

回复

要查看或添加评论,请登录

?????????????? ??????????????????的更多文章

  • Importance of LinkedIn: 7 Benefits to Polish Your Profile

    Importance of LinkedIn: 7 Benefits to Polish Your Profile

    Importance of LinkedIn: 7 Benefits to Polish Your Profile You've heard LinkedIn is important, but is it? And if so…

    5 条评论
  • What is Hibernate in Java? & Hibernate Interview Questions

    What is Hibernate in Java? & Hibernate Interview Questions

    What is Hibernate in Java? & Hibernate Interview Questions Hibernate is a Java-based persistence framework and an…

    3 条评论
  • Classloaders In Java

    Classloaders In Java

    Understand Classloading: Types Of Classloaders In Java 7 Types Of Class Loaders In Java In this article, we’ll…

  • 5 Basic concepts in programming languages for better coding skills

    5 Basic concepts in programming languages for better coding skills

    5 basic Concepts in programming languages for better coding skills Programming languages are the foundation of modern…

    2 条评论
  • Object-Oriented Programming (OOP) languages (Features & Advantages)

    Object-Oriented Programming (OOP) languages (Features & Advantages)

    Object-Oriented Programming (OOP) languages! (Features & Advantages) Object-Oriented Programming (OOP) is a programming…

  • What is Git?

    What is Git?

    What is Git? Git is a DevOps tool that is used as a version-control system for tracking the changes happening in system…

    2 条评论
  • Spring Boot Framework

    Spring Boot Framework

    What is Spring Boot? Spring Boot is a Framework from “The Spring Team” to ease the bootstrapping and development of new…

    2 条评论
  • Java Interview Questions for Freshers & Experienced

    Java Interview Questions for Freshers & Experienced

    Java Interview Questions for Freshers & Experienced Book Reference: https://www.tutorialspoint.

    2 条评论
  • What is Chat GPT ? & Use Of Chat GPT.

    What is Chat GPT ? & Use Of Chat GPT.

    What is Chat GPT ? & Use Of Chat GPT. Chat GPT-4 is a large-scale, multimodal model which can accept image and text…

    2 条评论
  • Java Multiple-Choice Questions

    Java Multiple-Choice Questions

    Java Multiple-Choice Questions Book Reference : https://www.tutorialspoint.

社区洞察

其他会员也浏览了