Mutable and Immutable in Java29 Mar 2025 | 5 min read Java is an object-oriented programming language. As it is an object-oriented programming language, it's all methods and mechanism revolves around the objects. One object-based concept is mutable and immutable in Java. Objects in Java are either mutable or immutable; it depends on how the object can be iterated. In this section, we will discuss mutable and immutable objects in Java. Further, we will see the difference between them. What are Mutable ObjectsThe mutable objects are objects whose value can be changed after initialization. We can change the object's values, such as field and states, after the object is created. For example, Java.util.Date, StringBuilder, StringBuffer, etc. When we made a change in existing mutable objects, no new object will be created; instead, it will alter the value of the existing object. These object's classes provide methods to make changes in it. The Getters and Setters ( get() and set() methods ) are available in mutable objects. The Mutable object may or may not be thread-safe. What are Immutable ObjectsThe immutable objects are objects whose value can not be changed after initialization. We can not change anything once the object is created. For example, primitive objects such as int, long, float, double, all legacy classes, Wrapper class, String class, etc. In a nutshell, immutable means unmodified or unchangeable. Once the immutable objects are created, its object values and state can not be changed. Only Getters ( get() method) are available not Setters ( set() method) for immutable objects. Let's see how to create classes for mutable and immutable objects. How to Create a Mutable ClassThe following two things are essential for creating a mutable class:
Consider the below example of the mutable class: Output: JavaTpoint Java Training From the above example, we are changing the name value using the setName method. How to Create an Immutable ClassThe following things are essential for creating an immutable class:
Consider the below example: Output: Core Java Training Hence, we have discussed the mutable and immutable objects and classes. Let's discuss the differences between them: Difference between Mutable and Immutable ObjectsThe following are some key difference between mutable and immutable objects in Java:
Consider the below table:
Why are Strings in Java ImmutableString in Java is a very special class, as it is used almost in every Java program. That's why it is Immutable to enhance performance and security. Let's understand it in detail: In Java, strings use the concept of literals. Suppose we have an object having many reference variables. In such a scenario, if we will change the value of a reference variable, it will affect the entire object and all of its values. Apart from the above reasons, the following reasons are also responsible for making the String immutable: Immutable objects are very simple; there is no need for synchronization and are inherently thread-safe. But, Immutable objects make good building blocks for other objects, so we have to provide them special care. Most Developers make the String the final object so that it can not be altered later. ConclusionWe have discussed mutable and immutable objects in Java, now, we have a clear understanding of both mutable and immutable objects and classes. Further, we have seen how to create a mutable and immutable class. It is noted that immutable objects can be changed to their value and state once initiated and may or may not be thread-safe. Comparatively, the immutable objects can not be changed to their state and value and default thread-safe. Next TopicDetect-cycle-in-a-directed-graph-in-java |
Binary tree is a tree type non-linear data structure that are mainly used for sorting and searching because they store data in hierarchical form. In this section, we will learn the implementation of binary tree data structure in Java. Also, provides a short description of...
64 min read
Sorting things in an array looks for arrangement of objects in an array is one of the basic problem type alent in computer science for applications in pattern matching techniques and simulations or data graphics and visualizations. One such task is to sort elements of some numerical...
8 min read
In Java, the Object class is the parent class of all the Java classes. Every Java class is a direct or indirect child of the Java Object class. Hence, every Java class extends the Object class. Therefore, we need not to write the following statement...
3 min read
Given a HexaDecimal Number N, converting the number to its corresponding Binary Coded Decimal is the task. Example 1: Input: String str = "2A3" Output: The equivalent BCD is 0010 1010 0011 Explanation: The Binary of 2: 0010 The Binary of A: 1010 The Binary of 3: 0011 Therefore, the equivalent BCD is 0010 1010 0011. Example...
6 min read
Logical calculations and programming both depend on XOR (exclusive OR) operations. The XOR operator in Java offers a quick and easy way to work with binary data and perform bitwise operations. The specifics of XOR operations in Java will be covered in full in this section,...
4 min read
In this section, we will learn how to save, compile, and run (execute) a Java program in Command Prompt (CMD) using notepad. Before running (execute) a Java program, ensure that Java is installed in the system and the path is properly set. If the path is...
3 min read
A pangram is a string of characters that have all the letters of the English alphabet at least once. This is a typical exercise in programming, because it aims at both string operations, data models, and algorithms. In this section, we will learn what exactly is...
4 min read
The regionMatches() method for the String class has two variations that can be utilized to test assuming two string districts are coordinating or equivalent. There are two variations of this strategy, i.e., one is ignore case, and the other is case sensitive. The regionMatches() method is utilized...
4 min read
Java 8 introduced a number of new features to the programming language, one of the most significant being the lambda expression. Lambda expressions provide a concise way to express a method that can be passed around as an argument to another method, enabling functional programming paradigms...
4 min read
Java is a powerful object-oriented programming language that provides developers with a wide range of tools and features to build robust and scalable applications. One of the features that make Java stand out is its support for generics. Generics allow developers to write generic classes and...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India