Are Java arrays mutable ?

0

 If you are working in java programming language and preparaing for technical interview then just read this article. It can help you in the interview.



What are mutable objects :

Mutable objects are those objects which can be changed after creation . While immutables are those whose values can not be changed once created. Mutable objects generally provide a method to modify the values. We use getter and setter methods . Exmaple StringBuilder class.


Are Java Arrays mutable :

Whenever this question is asked always remember two things in mind.

1. Length

2. Value of the index


Lets come to the first point ie. length. As we know that once we decare the length of the array then we can not change it's length. It is fixed once declared. So from this we can say that the length of the array is immutable. Remember we are only talking about length not whole array. If you want to change the array length then you have to allocate a new array. 

Now lets come to the second point that is value of the index. If you have ever written any code in java using array then you know that you can assign , edit the values of the array. It means that we can modify the array and we can change the values .


In java everything except string is mutable by default.So Yes java arrays are mutable in nature but the size with which they are created is final and you can not change size of the array once initialized. We know that a mutable is an object which state can be changed once it is created.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !