Immutable class Interview Questions:-


  What is an Immutable Object ?

Object that can't be changed after instantiation.


  What is an immutable class ?

Class using which only immutable (objects that cannot be changed after initialization) objects can be created.


  Why String is popular HashMap key in Java?

Since String is immutable, its hashcode is cached at the time of creation and it doesnít need to be calculated again. This makes it a great candidate for key in a Map and itís processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.


  Does Declaring an object "final" makes it immutable ?

Only declaring primitive types as final makes them immutable. Making objects final means that the object handler cannot be used to target some other object but the object is still mutable.


  Immutable objects are automatically thread-safe Ėtrue/false?

True. Since the state of the immutable objects can not be changed once they are created they are automatically synchronized/thread-safe.


  Which classes in java are immutable?

All wrapper classes in java.lang are immutable :-

String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger


  Why is String immutable in Java ?

There are some points of String immutable :-

1. String Pool :-When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

2. To Cache its Hashcode :- If string is not immutable, One can change its hashcode and hence not fit to be cached.

3. Security :- String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment.


  Why Char array is preferred over String for storing password?

String is immutable in java and stored in String pool. Once itís created it stays in the pool until unless garbage collected, so even though we are done with password itís available in memory for longer duration and there is no way to avoid it. Itís a security risk because anyone having access to memory dump can find the password as clear text.


  What are the advantages of immutability?

There are some advantages of immutability :-

1. Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided.

2. Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state.

3. The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction.

4. The best use of the immutable objects is as the keys of a map.


  How to create an immutable class?

To create an immutable class following steps should be followed :-

1.Create a final class.

2. Set the values of properties using constructor only.

3. Make the properties of the class final and private.

4. Do not provide any setters for these properties.

5. If the instance fields include references to mutable objects, don't allow those objects to be changed :-

   (a). Don't provide methods that modify the mutable objects.

   (b). Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.


  What are the advantages of Immutable Classes in Java?

There are many advantages of Immutable Classes :-

1. Immutable objects are by default thread safe, can be shared without synchronization in concurrent environment.

2. Immutable object simplifies development, because its easier to share between multiple threads without external synchronization.

3. Immutable object boost performance of Java application by reducing synchronization in code.

4. Another important benefit of Immutable objects is reusability, you can cache Immutable object and reuse them, much like String literals and Integers. You can use static factory methods to provide methods like valueOf(), which can return an existing Immutable object from cache, instead of creating a new one.


  Why Char array is preferred over String for storing password?

String is immutable in java and stored in String pool. Once itís created it stays in the pool until unless garbage collected, so even though we are done with password itís available in memory for longer duration and there is no way to avoid it. Itís a security risk because anyone having access to memory dump can find the password as clear text.


PHP

PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more