Java
Java is an object oriented programming language. Flow of Java source code can be seen as followings:
JRE Architecture
It includes JVM or JIT compiler where code gets executed.
JVM
JVM (Java Virtual Machine) is an abstract machine. Java Virtual Machine is created when you run a java program using java command e.g. java HelloWorld. JVM is responsible for converting byte code into machine specific code and that's why you have different JVM for Windows, Linux or Solaris but one JAR can run on all this operating system. Java Virtual machine is at heart of Java programming language and provide several feature to Java programmer including Memory Management and Garbage Collection, Security and other system level services. Java Virtual Machine can be customized e.g we can specify starting memory or maximum memory of heap size located inside JVM at the time of JVM creation. If we supplied invalid argument to java command it may refuse to create Java Virtual Machine by saying "failed to create Java virtual machine: invalid argument". In short Java Virtual Machine or JVM is the one who provides Platform independence to Java.
ClassLoader in Java
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.[1] Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders. Delegation is an important concept to understand when learning about classloaders.
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.
We know that Java Program runs on Java Virtual Machine (JVM). When we compile a Java Class, it transforms it in the form of bytecode that is platform and machine independent compiled program and store it as a .class file. After that when we try to use a Class, Java ClassLoader loads that class into memory.
There are three types of built-in ClassLoader in Java:
1. Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other core classes for example java.lang.* package classes
2. Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
3. System Class Loader – It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.
Note-: Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from
OOP
Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
Example of Iterator
Note-:HashSet uses HashMap internally to store it’s objects. Whenever you create a HashSet object, one HashMap object associated with it is also created. This HashMap object is used to store the elements you enter in the HashSet. The elements you add into HashSet are stored as keys of this HashMap object. The value associated with those keys will be a constant.
Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.
A class must implement Serializable interface present in java.io package in order to serialize its object successfully. Serializable is a marker interface that adds serializable behaviour to the class implementing it.
Java provides Serializable API encapsulated under java.io package for serializing and deserializing objects which include,
While serializing if you do not want any field to be part of object state then declare it either static or transient based on your need and it will not be included during java serialization process.
Serialization in java is implemented by ObjectInputStream and ObjectOutputStream, so all we need is a wrapper over them to either save it to file or send it over the network.
*************************************************************
If these methods are present in the class, they are used for serialization purposes.
Externalization
If you notice the java serialization process, it’s done automatically. Sometimes we want to obscure the object data to maintain it’s integrity. We can do this by implementing
Transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.
Note-:
For complete serialization: http://www.journaldev.com/2452/serialization-in-java
http://www.studytonight.com/java/serialization-and-deserialization.php
Java is an object oriented programming language. Flow of Java source code can be seen as followings:
JRE Architecture
1) Classloader
Classloader is a subsystem of JVM that is used to load class files.
2) Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
3) Heap
It is the runtime data area in which objects are allocated.
4) Stack
Java Stack stores frames.It holds local variables and partial results, and plays a part in method invocation and return. |
Each thread has a private JVM stack, created at the same time as thread. |
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes. |
5) Program Counter Register
PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed.
6) Native Method Stack
It contains all the native methods used in the application.
7) Execution Engine
JVM
JVM (Java Virtual Machine) is an abstract machine. Java Virtual Machine is created when you run a java program using java command e.g. java HelloWorld. JVM is responsible for converting byte code into machine specific code and that's why you have different JVM for Windows, Linux or Solaris but one JAR can run on all this operating system. Java Virtual machine is at heart of Java programming language and provide several feature to Java programmer including Memory Management and Garbage Collection, Security and other system level services. Java Virtual Machine can be customized e.g we can specify starting memory or maximum memory of heap size located inside JVM at the time of JVM creation. If we supplied invalid argument to java command it may refuse to create Java Virtual Machine by saying "failed to create Java virtual machine: invalid argument". In short Java Virtual Machine or JVM is the one who provides Platform independence to Java.
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.[1] Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders. Delegation is an important concept to understand when learning about classloaders.
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.
We know that Java Program runs on Java Virtual Machine (JVM). When we compile a Java Class, it transforms it in the form of bytecode that is platform and machine independent compiled program and store it as a .class file. After that when we try to use a Class, Java ClassLoader loads that class into memory.
There are three types of built-in ClassLoader in Java:
1. Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other core classes for example java.lang.* package classes
2. Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
3. System Class Loader – It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.
Note-: Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from
java.lang
package) and other runtime classes to the memory first. Most classloaders delegate finding classes and resources to their parent before searching their own classpath. If the parent classloader cannot find a class or resource, only then does the classloader attempt to find them locally. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
Static keyword
String vs String Buffer vs String Builder
1) All these three classes are members of java.lang package and they are final classes. That means you can’t create subclasses to these three classes.
Collection of objects is called class. It is a logical entity.
Abstraction
Encapsulation
Encapsulation is the technique used to implement abstraction in object oriented programming. Encapsulation is used for access restriction to a class members and methods.Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using private
, protected
and public
keywords.
private
, protected
and public
keywords.Inheritance
Inheritance in java is a mechanism in which one object acquires the properties and behaviors of parent object. It’s essentially creating parent-child relationship between classes. In java, you will use inheritance mainly for code re-usability and maintainability.
Polymorphism
Polymorphism is the ability by which, we can create functions or reference variables which behaves differently in different programmatic context.
In java language, polymorphism is essentially considered into two versions:
- Compile time polymorphism (static binding or method overloading)
- Runtime polymorphism (dynamic binding or method overriding)
Eg:
An example of polymorphism is referring the instance of subclass, with reference variable of super-class. e.g.
Object o = new Object(); //o can hold the reference of any subtype Object o = new String(); Object o = new Integer();
Compile time polymorphism (static binding or method overloading)
As the meaning is implicit, this is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.
In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both.
- Overloaded methods must have different argument list
- May have different return types if argument list is different.
- May throw different exceptions
- May have different access modifiers
Runtime polymorphism (dynamic binding or method overriding)
Runtime polymorphism is essentially referred as method overriding. Method overriding is a feature which you get when you implement inheritance in your program. Runtime polymorphism is achieved using method overriding. Rules for method overriding are as follows.
- Overriding method argument list must match the overridden method
- The return type must be the same or subtype of overridden method
- access level cannot be more restrictive than overridden method
Important points:
- Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
- In java, polymorphism is divided into two parts : method overloading and method overriding.
- Some may argue that method overloading is not polymorphism. Then what does the term compile time “polymorphism” means??
- Another term operator overloading is also there, e.g. “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your own custom defined operator overloading in java.
- Variable or Methods marked static belong to the Class rather then to any particular Instance.
- Static Method or variable can be used without creating or referencing any instance of the Class.
- If there are instances, a static variable of a Class will be shared by all instances of that class, This will result in only one copy.
- A static Method can’t access a non static variable nor can directly invoke non static Method (It can invoke or access Method or variable via instances).
- Static Methods can not be overriden as they are Class specific and don’t belong to an Instance.
- Static Methods can be redefined.
- If a Class contains any static blocks then that block will be executed only when the Class is loaded in JVM. Creating multiple instances does not execute the static block multiple time. Only the constructor will be executed multiple time.
- If Class.forName(“class_name“) is called then the static block of the Class will get executed
The Static keyword can be applied to-:
- Method
- Variable
- Class nested within another Class
- Initialization Block
Order of Execution Initialization Block, Static Initialization Block and Constructor
- Initialization blocks run in the order they appear in the program
- Static initialization blocks run when class is loaded in JVM
- Instance initialization blocks run every time a new instance is created.
- Instance initialization blocks run AFTER the super constructor has completed executing and BEFORE current class constructor.
- They can be used to perform operations those are common to constructors.
String vs String Buffer vs String Builder
1) All these three classes are members of java.lang package and they are final classes. That means you can’t create subclasses to these three classes.
2) All three classes implement Serializable and CharSequence interface.
3) java.lang.String objects are immutable in java. That is, once you create String objects, you can’t modify them. Whenever you try to modify the existing String object, a new String object is created with modifications. Existing object is not at all altered. Where as java.lang.StringBuffer and java.lang.StringBuilder objects are mutable. That means, you can perform modifications to existing objects.
4) Only String and StringBuffer objects are thread safe. StringBuilder objects are not thread safe. So whenever you want immutable and thread safe string objects, use java.lang.String class and whenever you want mutable as well as thread safe string objects then use java.lang.StringBuffer class.
5) In all three classes, toString() method is overrided. So. whenever you use reference variables of these three types, they will return contents of the objects not physical address of the objects.
6) hashCode() and equals() methods are overrided only in java.lang.String class but not in java.lang.StringBuffer and java.lang.StringBuilder classes.
7) There is no reverse() and delete() methods in String class. But, StringBuffer and StringBuilder have reverse() and delete() methods.
8) In case of String class, you can create the objects without new operator. But in case of StringBuffer and StringBuilder class, you have to use new operator to create the objects.
Another example of implementation is
Hashcode another typical implemntation
@Override
public int hashCode()
{
final int PRIME = 31;
int result = 1;
result = PRIME * result + getId();
return result;
}
Collection Framework
Before proceeding we must understand Collection interface
Note-:equals() and hashcode() methods in the Collection interface are not the methods of java.lang.Object class. Because, interfaces does not inherit from Object class. Only classes in java are sub classes of Object class. Any classes implementing Collection interface must provide their own version of equals() and hashcode() methods or they can retain default version inherited from Object class.
Method of List Interface
4) Only String and StringBuffer objects are thread safe. StringBuilder objects are not thread safe. So whenever you want immutable and thread safe string objects, use java.lang.String class and whenever you want mutable as well as thread safe string objects then use java.lang.StringBuffer class.
5) In all three classes, toString() method is overrided. So. whenever you use reference variables of these three types, they will return contents of the objects not physical address of the objects.
6) hashCode() and equals() methods are overrided only in java.lang.String class but not in java.lang.StringBuffer and java.lang.StringBuilder classes.
7) There is no reverse() and delete() methods in String class. But, StringBuffer and StringBuilder have reverse() and delete() methods.
8) In case of String class, you can create the objects without new operator. But in case of StringBuffer and StringBuilder class, you have to use new operator to create the objects.
String Constant Pool
We all know that JVM divides the allocated memory to a Java program into two parts. one is Stack and another one is heap. Stack is used for execution purpose and heap is used for storage purpose. In that heap memory, JVM allocates some memory specially meant for string literals. This part of the heap memory is called String Constant Pool.
Whenever you create a string object using string literal, that object is stored in the string constant pool and whenever you create a string object using new keyword, such object is stored in the heap memory.
One more interesting thing about String Constant Pool is that, pool space is allocated to an object depending upon it’s content. There will be no two objects in the pool having the same content.
This is what happens when you create string objects using string literal,
“When you create a string object using string literal, JVM first checks the content of to be created object. If there exist an object in the pool with the same content, then it returns the reference of that object. It doesn’t create new object. If the content is different from the existing objects then only it creates new object.”
http://javaconceptoftheday.com/how-the-strings-are-stored-in-the-memory/Whenever you create a string object using string literal, that object is stored in the string constant pool and whenever you create a string object using new keyword, such object is stored in the heap memory.
One more interesting thing about String Constant Pool is that, pool space is allocated to an object depending upon it’s content. There will be no two objects in the pool having the same content.
This is what happens when you create string objects using string literal,
“When you create a string object using string literal, JVM first checks the content of to be created object. If there exist an object in the pool with the same content, then it returns the reference of that object. It doesn’t create new object. If the content is different from the existing objects then only it creates new object.”
Equals and hashcodes in core java
What is hashcode exactly? Wikipedia defines then as follows.
A hash function is an algorithm that maps data of variable length to data of fixed length. The values returned by hash functions are called hash values, hash codes, hash sums, check sums or simply hashes.
Java hashcode() method does the same, but there are some other points to be considered while studying hashcodes.
Hashcodes are typically used to enhance the performance of large collections of data.
Collections such as HashSet and HashMap use the hashcode of an object to determine how exactly an object should be stored in the collection.
When an object is to be searched from collections that use hashcodes, object’s hashcode is calculated first and with the help of it, object is retrieved.
Example of implementation of equals()
Example of implementation of hashcode()
A hash function is an algorithm that maps data of variable length to data of fixed length. The values returned by hash functions are called hash values, hash codes, hash sums, check sums or simply hashes.
Java hashcode() method does the same, but there are some other points to be considered while studying hashcodes.
Hashcodes are typically used to enhance the performance of large collections of data.
Collections such as HashSet and HashMap use the hashcode of an object to determine how exactly an object should be stored in the collection.
When an object is to be searched from collections that use hashcodes, object’s hashcode is calculated first and with the help of it, object is retrieved.
Here are two rules that are good to know about implementing the
hashCode()
method in your own classes, if the hashtables in the Java Collections API are to work correctly:- If object1 and object2 are equal according to their
equals()
method, they must also have the same hash code. - If object1 and object2 have the same hash code, they do NOT have to be equal too.
Example of implementation of equals()
public class Employee { protected long employeeId; protected String firstName; protected String lastName; }
public class Employee { ... public boolean equals(Object o){ if(o == null) return false; if(!(o instanceof) Employee) return false; Employee other = (Employee) o; if(this.employeeId != other.employeeId) return false; if(! this.firstName.equals(other.firstName)) return false; if(! this.lastName.equals(other.lastName)) return false; return true; } }
public class Employee { protected long employeeId; protected String firstName; protected String lastName; public int hashCode(){ return (int) employeeId * firstName.hashCode() * lastName.hashCode(); } }
Another example of implementation is
class
Price{
private
String item;
private
int
price;
public
int
hashCode(){
System.out.println(
"In hashcode"
);
int
hashcode =
0
;
hashcode = price*
20
;
hashcode += item.hashCode();
return
hashcode;
}
public
boolean
equals(Object obj){
System.out.println(
"In equals"
);
if
(obj
instanceof
Price) {
Price pp = (Price) obj;
return
(pp.item.equals(
this
.item) && pp.price ==
this
.price);
}
else
{
return
false
;
}
}
}
Hashcode another typical implemntation
@Override
public int hashCode()
{
final int PRIME = 31;
int result = 1;
result = PRIME * result + getId();
return result;
}
The entire collection framework is divided into four interfaces.
1) List —> It handles sequential list of objects. ArrayList, Vector and LinkedList classes implement this interface.
2) Queue —> It handles special list of objects in which elements are removed only from the head. LinkedList and PriorityQueue classes implement this interface.
3) Set —> It handles list of objects which must contain unique element. This interface is implemented by HashSet and LinkedHashSet classes and extended by SortedSet interface which in turn, is implemented by TreeSet.
4) Map —> This is the one interface in Collection Framework which is not inherited from Collection interface. It handles group of objects as Key/Value pairs. It is implemented by HashMap and HashTable classes and extended by SortedMap interface which in turn is implemented by TreeMap.
Three of above interfaces (List, Queue and Set) inherit from Collection interface. Although, Map is included in collection framework it does not inherit from Collection interface.
Before proceeding we must understand Collection interface
Note-:equals() and hashcode() methods in the Collection interface are not the methods of java.lang.Object class. Because, interfaces does not inherit from Object class. Only classes in java are sub classes of Object class. Any classes implementing Collection interface must provide their own version of equals() and hashcode() methods or they can retain default version inherited from Object class.
Method of List Interface
SL NO | Methods | Descriptions |
1 | E get(int index) | Returns element at the specified position. |
2 | E set(int index, E element) | Replaces an element at the specified position with the passed element. |
3 | void add(int index, E element) | Inserts passed element at a specified index. |
4 | E remove(int index) | Removes an element at specified index. |
5 | int indexOf(Object o) | It returns an index of first occurrence of passed object. |
6 | int lastIndexOf(Object o) | It returns an index of last occurrence of passed object. |
7 | ListIterator<E> listIterator() | It returns a list iterator over the elements of this list. |
8 | ListIterator<E> listIterator(int index) | Returns a list iterator over the elements of this list starting from the specified index. |
9 | List<E> subList(int fromIndex, int toIndex) | Returns sub list of this list starting from ‘fromIndex’ to ‘toIndex’. |
Methods Of Queue Interface:
Here are the methods of Queue interface. Some of the methods throw an exception if operation is not possible and some methods return a value (null or false) if operation is not possible.
Operation | Throws An Exception If operation is not possible | Returns null or false if operation is not possible |
Add an element to the queue. | add() | offer() |
Retrieve an element from the head of the queue. | element() | peek() |
Retrieve And Remove an element from the head of the queue. | remove() | poll() |
Methods Of Set Interface :
All methods are inherited from Collection interface. SortedSet interface adds method to this interface. Below are some of the point to remember regarding Sets.
- Set contains only unique elements. It does not allow duplicates.
- Set can contain only one null element.
- Random access of elements is not possible.
- Order of elements in a set is implementation dependent. HashSet elements are ordered on hash code of elements. TreeSet elements are ordered according to supplied Comparator (If no Comparator is supplied, elements will be placed in ascending order) and LinkedHashSet maintains insertion order.
- Set interface contains only methods inherited from Collection interface. It does not have it’s own methods. But, applies restriction on methods so that duplicate elements are always avoided.
- One more good thing about Set interface is that the stronger contract between equals() and hashCode() methods. According to this contract, you can compare two set instances of different implementation types (HashSet, TreeSet and LinkedHashSet).
- Two set instances, irrespective of their implementation types, are said to be equal if they contain same elements.
There are other but these are important ones ;
Method | Description |
---|---|
void clear() | It is used to remove all of the mappings from this map. |
boolean containsKey(Object key) | It is used to return true if this map contains a mapping for the specified key. |
boolean containsValue(Object value) | It is used to return true if this map maps one or more keys to the specified value. |
boolean isEmpty() | It is used to return true if this map contains no key-value mappings. |
Object clone() | It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned. |
Set entrySet() | It is used to return a collection view of the mappings contained in this map. |
Set keySet() | It is used to return a set view of the keys contained in this map. |
Object put(Object key, Object value) | It is used to associate the specified value with the specified key in this map. |
int size() | It is used to return the number of key-value mappings in this map. |
Collection values() | It is used to return a collection view of the values contained in this map. |
Example of Iterator
Iterator<String> it = collection_ref.iterator();
while
(it.hasNext())
{
System.out.println(it.next());
//Removing an element from list
it.remove();
}
Note-:HashSet uses HashMap internally to store it’s objects. Whenever you create a HashSet object, one HashMap object associated with it is also created. This HashMap object is used to store the elements you enter in the HashSet. The elements you add into HashSet are stored as keys of this HashMap object. The value associated with those keys will be a constant.
Exception Handling
Types of Exception
There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun microsystem says there are three types of exceptions:
1) Checked Exceptions :
Checked exceptions are the exceptions which are known during compile time. These are the exceptions that are checked at compile time. They are also called compile time exceptions.
These exceptions must be handled either using try-catch blocks or using throws clause. If not handled properly, they will give compile time error.
All sub classes of java.lang.Exception except sub classes of RunTimeException are checked exceptions.
2) Unchecked Exceptions :
Unchecked exceptions are the exceptions which are known at run time. They can not be known at compile time because they occur only at run time. That’s why they are also called Run Time Exceptions.
All the sub classes of RunTimeException and all sub classes of Error class are unchecked exceptions.If any statement in the program throws unchecked exceptions and you are not handling them either using try-catch blocks or throws clause, then it does not give compile time error. Compilation will be successful but program may fail at run time. Therefore, to avoid premature termination of the program, you have to handle them properly.Note-:
- System.exit() is the only way when finally block will not get executed coz in that case JVM will shut down.
- Custom Exception can also be created by extending Exception class.
- Catch block should be order in the form of most specific to most general. Otherwise compiler will complain for unreachable code
Serialization
Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.
A class must implement Serializable interface present in java.io package in order to serialize its object successfully. Serializable is a marker interface that adds serializable behaviour to the class implementing it.
Java provides Serializable API encapsulated under java.io package for serializing and deserializing objects which include,
- java.io.serializable
- java.io.Externalizable
- ObjectInputStream
- and ObjectOutputStream etc.
If you want a class object to be serializable, all you need to do it implement the java.io.Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement. It’s like an Opt-In process through which we make our classes serializable.
While serializing if you do not want any field to be part of object state then declare it either static or transient based on your need and it will not be included during java serialization process.
Serialization in java is implemented by ObjectInputStream and ObjectOutputStream, so all we need is a wrapper over them to either save it to file or send it over the network.
Let’s see a simple Serialization in java program example.
*******************************************************
import java.io.Serializable;
public class Employee implements Serializable
{
public String firstName;
public String lastName;
private static final long serialVersionUID = 5462223600l;
}
*****************************************************
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializaitonClass {
public static void main(String[] args) {
Employee emp = new Employee();
emp.firstName = "Vivekanand";
emp.lastName = "Gautam";
try {
FileOutputStream fileOut = new FileOutputStream("./employee.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(emp);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in ./employee.txt file");
} catch (IOException i) {
i.printStackTrace();
}
}
}
*************************************************************
import java.io.*;
public class DeserializationClass {
public static void main(String[] args) {
Employee emp = null;
try {
FileInputStream fileIn = new FileInputStream("./employee.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (Employee) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserializing Employee...");
System.out.println("First Name of Employee: " + emp.firstName);
System.out.println("Last Name of Employee: " + emp.lastName);
}
}
public static void main(String[] args) {
Employee emp = null;
try {
FileInputStream fileIn = new FileInputStream("./employee.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (Employee) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserializing Employee...");
System.out.println("First Name of Employee: " + emp.firstName);
System.out.println("Last Name of Employee: " + emp.lastName);
}
}
Java Serialization Methods
We have seen that serialization in java is automatic and all we need is implementing Serializable interface. The implementation is present in the ObjectInputStream and ObjectOutputStream classes. But what if we want to change the way we are saving data, for example we have some sensitive information in the object and before saving/retrieving we want to encrypt/decrypt it. That’s why there are four methods that we can provide in the class to change the serialization behavior.If these methods are present in the class, they are used for serialization purposes.
- readObject(ObjectInputStream ois): If this method is present in the class, ObjectInputStream readObject() method will use this method for reading the object from stream.
- writeObject(ObjectOutputStream oos): If this method is present in the class, ObjectOutputStream writeObject() method will use this method for writing the object to stream. One of the common usage is to obscure the object variables to maintain data integrity.
- Object writeReplace(): If this method is present, then after serialization process this method is called and the object returned is serialized to the stream.
- Object readResolve(): If this method is present, then after deserialization process, this method is called to return the final object to the caller program. One of the usage of this method is to implement Singleton pattern with Serialized classes. Read more at Serialization and Singleton.
Externalization
If you notice the java serialization process, it’s done automatically. Sometimes we want to obscure the object data to maintain it’s integrity. We can do this by implementing
java.io.Externalizable
interface and provide implementation of writeExternal() and readExternal() methods to be used in serialization process
transient
Note-:
- Serialization interface needs to be implemented in order to make object serialized.
- Transient instance variable doesn’t serialized with Object state.
- If Super class implements Serializable then sub class are also Serializable automatically.
- If Super class is not serializable then when sub class is de serialized then super class’s default constructor will be invoked. Hence all variable will get default value and reference will be null.
- static variable values are also not serialized since they belongs to class and not object.
For complete serialization: http://www.journaldev.com/2452/serialization-in-java
http://www.studytonight.com/java/serialization-and-deserialization.php