Explain serialVersionUID in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Explain serialVersionUID in Java

What is serialVersionUID and what is its need ?

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.

Continue reading “Explain serialVersionUID in Java”

Explain Externalizable in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 5 Comments on Explain Externalizable in Java

Explain Externalizable in Java

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 :

  1. void readExternal(ObjectInput in)
  2. void writeExternal(ObjectOutput out)

Continue reading “Explain Externalizable in Java”

Complete Java Collection tutorial for the beginner

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 15 Comments on Complete Java Collection tutorial for the beginner

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

Continue reading “Complete Java Collection tutorial for the beginner”

Java – J2EE Interview Questions – 1

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , with 4 Comments on Java – J2EE Interview Questions – 1

JAVA – J2EE Interview Questions – 1, JAVA – J2EE Interview Questions – 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap

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


Continue reading “Java – J2EE Interview Questions – 1”

Exception and Error handling framework in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with Leave a comment on Exception and Error handling framework in Java

Explain the concept of 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 Continue reading “Exception and Error handling framework in Java”

Create Executable jar file of classes in package

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 4 Comments on Create Executable jar file of classes in package

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*

Continue reading “Create Executable jar file of classes in package”

Compile and run Java program in package from command line

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 33 Comments on Compile and run Java program in package from command line

How to Compile 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

Continue reading “Compile and run Java program in package from command line”

Project Coin – Java 7 new features

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Project Coin – Java 7 new features

What is Project Coin and how it is related to JAVA

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. Continue reading “Project Coin – Java 7 new features”

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

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 37 Comments on What is the need to Override Hashcode() and equals() method

Although there are lots of material 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.

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. Continue reading “What is the need to Override Hashcode() and equals() method”

Create XML File using DOM Parser of JAXP and Transformation APIs

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , , with 3 Comments on Create XML File using DOM Parser of JAXP and Transformation APIs

Example of Creating XML File using JAXP and Transformation APIs

We have already studied about :

  1. What is DOM and its structure
  2. JAXP for DOM Parsers

We will create a XML file using DOM parser and Transformation APIs in this article. Continue reading “Create XML File using DOM Parser of JAXP and Transformation APIs”