The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named “serialVersionUID” that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class.serialVersionUID fields are not useful as inherited members.
We have seen an example of Serialization and how it works in our previous article. However there is one more interface provided by the Java API which is “Externalizable“. This interface extends “Serializable” and provides two more methods as shown below :
Collection represents the group of objects. Depending on the method of storing and retrieving, collections are basically divided into three parts – Set, Map and List. Where Set does not contain duplicate values, Map contains key value type of data whereas List can have a duplicate values stored in it sequentially. This framework is provided in “java.util” package. Collection is the parent interface of all collections in java.
Following list describes the core collection interfaces.
1. What is Generic Servlet ? Answer : Defines a generic, protocol-independent servlet. GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy. To use Protocol specific servlet like http protocol, we can use HttpServlet. Difference in HttpServlet and GenericServlet is that, HttpServlet provides few more methods like doGet and doPost. Same operations can be done in service method of generic servlet. Read Servlet Life cycle
2. Can abstract class have Constructor ? Answer : Yes. abstract class can have a constructor.
3. We cannot instantiate the abstract class, then what is the need of constructor and how it is called. Answer : You may have not come across this situation. but answer i very easy. If want to initialize few parameters for all the subclasses then we can use the constructor of abstract class. Constructor calling mechanism is same as normal inheritance in java. Default constructor gets called automatically and to call other constructors we have to explicitly call it by using keyword “super”.
The topic is very easy and i am sure that lots of java programmer already know how to compile java program existing in package. However there are users who frequently works on eclipse, netbeans or any other IDE and don’t know that how the program actually works behind the IDE. So this article basically emphasizes on basics of Java Compilation.
After reading the title “project Coin“, few of you must be thinking that what is it ? And how it is related to Java 7 new features.
So, here we go…
Project Coin is the part of Java 7 development which is running since January 2009 with the aim of coming with small changes to the Java language.
In February to march 2009, there were nearly 70 proposals were submitted for huge range of possible changes in Java 7. Project coin has set up the example that how language can be developed in the future by discussion in openly manner with the public all around the world.
Although there are lots of materials are available on internet and API document about the necessity of the overriding the hashcode() and equals() method in Java but lots of new developers still not able to understand the necessity of hashcode() method.
In this article, I will try to explain step by step the need of overriding hashcode() method in Java.
Few Thump rules:
If two objects are same then they must return same value in hashcode() and equals() method whenever invoked.
It is not necessary that two different object must have different hashcode values. it might be possible that they share common hash bucket.
JVM assigns unique hashcode value to each object when they are created in memory and if developers don’t override the hashcode method then there is no way the two object returns same hashcode value.
As the question comes in your mind that equals() method is used to compare objects that they are having same value or not but why should we override the hashcode method ?
The answer to the question is for the hash technique based data structures like HashMap and HashTable.