Interesting Questions

Why map is not a collection?

collection acts upon values. map acts on key value pair

Why the finalize method is protected?

Finalize method is for the JVM to execute before GC.imagine if finalize is public then any object can call directly which is not what we wanted. If you have some open connection you can write cleanup codes in the finalize method in your class and leave the rest of the work to the JVM to call the finalize method.

The finalize method is made protected for the subclass to override it or any finalize operations.

Can a finalize method be called for twice?

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the <code>finalize</code> method causes the finalization of this object to be halted, but is otherwise ignored.

why the clone is protected?

clone is protected because it is something that ought to be overridden so that it is specific to the current class.It’s protected so you don’t use the one in object (it’ll just throw an exception anyway). They want you to override it in a class, then you make it public

How to convert BigDecimal to Integer?

BigDecimal b = new BigDecimal("123");

Integer i = new Integer(String.valueOf(b));

Why Serializable and Cloneable is not having anything?

Serializable and Cloneable are called marker or tag interface because they tag all derived class on purpose. Java has a default clone() method in Object class which should not be applied for all the class. So whenever user creates a class the object is not cloneable. The object is cloneable only when it has the marking of Cloneable interface. if any class implements Cloneable then it is understood by the JVM that the object is eligible for cloning and Object by default provides the cloning method this also provide the class to implement their own clone operation by making it protected

The class is marked Serializable for the JVM to understand that the class can be serialized


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s