Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (1.5k points)
I was attending an interview and the interviewer asked me this question and I got half right.

Can anybody please explain me in Layman's term that what is mutable and immutable in Java.

2 Answers

0 votes
by (7.2k points)
What are Mutable Objects

The 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.

What are Immutable Objects

The 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.
0 votes
by (1.4k points)

Mutable Object: Objects in which fields and states can be modified after the object is produced are referred to as Mutable Objects. Ex: Java.util.Date, StringBuilder. 

Immutable Object: Objects in which nothing can be modified after the object has been formed are known as Permanent objects. Examples: Boxed primitive objects such as Integer, Long, etc. 

In Java, immutable strinexits mutable doesn’t.

To learn more about Java please check out the dedicated blog for that

Related questions

0 votes
1 answer
asked Feb 23, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Mar 31, 2021 in Java by Jake (7k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...