Explain serialVersionUID in Java

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.

Published
Categorized as JAVA Tagged

Complete Java Collection tutorial for the beginner

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.

 

Java Collection Interfaces
Java Collection Interfaces

Published
Categorized as JAVA Tagged

Java – J2EE Interview Questions – 1

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


Published
Categorized as JAVA Tagged ,

Exception and Error handling framework in Java

In Java, the base class of all the Exceptions are “java.lang.Throwable“. This class has two subclasses – “Error” and “Exception“.

JAVA Exception Framework
JAVA Exception Framework

java.lang.Error indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
Examples : java.lang.ThreadDeath and java.lang.VirtualMachineError

Published
Categorized as JAVA Tagged

Create Executable jar file of classes in package

For the source code, please refer this article.

As per previous article, i am considering that you have already compiled the program.

go to the folder “classes” and create a manifest file named as “manifest.txt” and add the name of starting java class as shown below.

Main-Class: com.g2.ExecJar.Start

or, run below command in command prompt

echo Main-Class: com.g2.ExecJar.Start >manifest.txt

now, run the command to create a jar file:

jar cvfm First.jar manifest.txt comg2ExecJar*

Published
Categorized as JAVA Tagged

Compile and run Java program in package from command line

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.

Consider following directory structure :

Java Directory structure
Java Directory structure
Published
Categorized as JAVA Tagged

Project Coin – Java 7 new features

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.

Published
Categorized as JAVA Tagged

What is the need to Override Hashcode() and equals() method

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.

Published
Categorized as JAVA Tagged