Posts

Showing posts from September, 2012

Serializable

Serializable is a marker interface which is used to activate serialization on a class. Java Code package demo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Vehicle implements Serializable {     private static final long serialVersionUID = 1L;     private String modelName;     private int noOFWheels;     private String color;     public String getModelName() {         return modelName;     }     public void setModelName(String modelName) {         this.modelName = modelName;     }     public int getNoOFWheels() {...

Marker Interface

Marker interface is one which don't contain any methods and also it is specially recognized by JVM to provide some specific kind of functionality. Some of the Marker interfaces in java: 1. Serializable 2. Cloneable 3. EventListener

Cloneable

Cloneable interface is a marker interface which used for marking a class so that its objects can be cloned. If a class is not marked as cloneable and we try to clone its objects then "CloneNotSupportedException" will be thrown. Types of cloning in java: Shallow Cloning (Shallow Copy) In Shallow copy all the fields are just copied from source object to destination object. If the source object is containing any field that holds a reference to another object then only the reference will be copied i.e. source and destination objects field(that particular field) will point to same memory location. Deep Cloning (Deep copy) In Deep copy all the fields are deeply copied from source object to destination object. If the source object is containing any field that holds a reference to another object then for the destination object a new copy will be created i.e. source and destination objects field will point to different memory location.

Singleton

A singleton class is one which can be instantiated only once. This is useful when exactly one object is needed to coordinate actions across the system. Steps to acheive singleton class 1. Make constructor private. 2. Add a synchronized method to provide instance. 3. Disable cloning of current class object. Java code for singleton class package test; class Fruit {     private String name;     private static Fruit instance = null;     private Fruit() {         name = null;     }     public static synchronized Fruit getInstance() {         if(instance == null)             instance = new Fruit();         return instance;     }     public String getName() {         r...

Hibernate Demo Annotation Example

Image
Explained below is the steps for creating a sample of hibernate based project using annotations. Project Details Project Structure File Details Product.java Pojo class for Product. hibernate.cfg.xml Hibernate Configuration File. Main.java It will control the overall process. Java Code Product.java package demo.annotation; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="Product") public class Product {             @Id             @Column(name = "productId")             @Gen...

Hibernate Demo Example

Image
Explained below is the steps for creating a sample of hibernate based project. Project Details Project Structure File Details Product.java Pojo class for Product. Product.hbm.xml Mappings file for Product class. hibernate.cfg.xml Hibernate Configuration File. Main.java It will control the overall process. Java Code Product.java package demo.simple; public class Product {     private int productId;     private String productName;     private int productPrice;         public int getProductId() {         return productId;     }     public void setProductId(int productId) {         this.productId = productId;     }   ...

Shallow Copy vs Deep Copy

Shallow Cloning (Shallow Copy) In Shallow copy all the fields are just copied from source object to destination object. If the source object is containing any field that holds a reference to another object then only the reference will be copied i.e. source and destination objects field(that particular field) will point to same memory location. Deep Cloning (Deep copy) In Deep copy all the fields are deeply copied from source object to destination object. If the source object is containing any field that holds a reference to another object then for the destination object a new copy will be created i.e. source and destination objects field will point to different memory location.

Serialization

It’s a mechanism by which you can save or transfer the state of an object by converting it to a byte stream. It’s required to send the state of an object over a network through a socket. Serialization in Java It can be done by implementing Serialiazable interface. Note: Serialiazable interface is a marker interface. Serializable vs Externalizable Serializable Externalizable Marker interface Contains methods readExternal() and writeExternal() Inbuilt serialization mechanism Serialization mechanism can be defined using readExternal() and writeExternal(). Serialization performance optimization Unwanted or non Serializable attributes should be marked as transient. Save only the state of the object, not the derived attributes. Don’t serialize objects with default values. Serial Version UID It represents class version. During serialization it is associated with serialized class and thus it is required at the time of deserialization for checking of compat...

Comparable vs Comparator

Comparable interface is used to control the natural ordering of objects. + compareTo(Object) : int Comparator interface is used to sort objects in an order other than their natural ordering. + compare(Object, Object) : int + equals(Object) : boolean Example import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Person implements Comparable {     String name;     public Person(String name)     {         this.name = name;     }     public int compareTo(Object obj)     {         Person person = (Person) obj;         return this.name.compareTo(person.name);     }     @Override     public String toString()     {             retu...

Hibernate Many to Many Annotation Mapping

Image
Relationship Diagram Project Structure     File Name Use Language.java Pojo class with mapping for Language. Person.java Pojo class with mapping for Person. hibernate.cfg.xml Hibernate Configuration File. Main.java It will control the overall process. Language.java package manyToMany.annotation; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="LANGUAGE") public class Language  { @Id @Column(name = "LANGUAGE_ID") @GeneratedValue private long langId; @Column(name = "LANGUAGE_NAME") private String langName; public Language() { }  //no argument constructor  public Language(String langName) { this.langName = langName; }  public long getLangId() { return langId; ...

Hibernate Many to Many Mapping

Image
Relationship Diagram Project Structure File Name Use Language.java Pojo class for Language. Person.java Pojo class for Person. Language.hbm.xml Mappings file for Language class. Person.hbm.xml Mappings file for Person class. hibernate.cfg.xml Hibernate Configuration File. Main.java It will control the overall process. Language.java package manyToMany.simple; public class Language { private long langId; private String langName; public Language() { }  //no argument constructor public Language(String langName) { this.langName = langName; } public long getLangId() { return langId; } public void setLangId(long langId) { this.langId = langId; } public String getLangName() { return langName; } public void setLangName(String langName) { this.langName = langName; } } Person.java package manyToMany.simple; import java.util.HashSet; ...