Monday, February 21, 2011

Interview questions -- Serializable

Suppose I have 2 classes
1) Person having person name = "Yogesh";
2) User having username = "User" extends Person and implements Serializable.

What will be the values printed if I serialize and deserialize the User object?

I guessed it wrongly and answered that personname will not get printed as Person class is not serializable. BUT THAT WAS WRONG !!!

Both the values will be stored and retrieved correctly. The reason I don't know, but, this scenario, I have tested on PC

Saturday, February 19, 2011

What is serialVersionUID?

Source :

What is serialVersionUID?

Most people learn about serialVersionUID after they write their first serializable object (I know I did). You add ‘implements Serializable’ and in the next moment your IDE starts complaining… so what’s up?

Lets look at a simple example to see what meaning that variable has. In the example we will use the class SerializeMe shown below:

class SerializeMe implements Serializable {
    private static final long serialVersionUID = 1L;

    private int data;

    public SerializeMe (int data) { = data;

    public int getData() {
        return data;

Field data represents some information stored in the class. Class implements the Serializable interface, so my IDE automatically offered me to declare the serialVersionUID field. Lets start with value 1 set there.

We will also need some class that will serialize and deserialize the SerializeMe class. Here it is:

public class UIDTester {
    public static void main(String... strings) throws Exception {
        File file = new File("out.ser");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        SerializeMe serializeMe = new SerializeMe(1);

        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);

        SerializeMe dto = (SerializeMe) ois.readObject();
        System.out.println("data : " + dto.getData());

This code will serialize an instance of class SerializeMe to a file and then deserialize it back. Now let’s run the main function! You should get output that says:

data : 1

Tuesday, February 15, 2011

Best practices in Collections

If you do need a synchronized map, you can use the static method synchronizedMap(...) in class Collections to add synchronization to a regular map. Don't use the legacy collection classes, such as Hashtable. Example:

// A regular, unsynchronized map  
Map<Integer, String> map = new HashMap<Integer, String>();  
// Wrap it to add synchronization  
Map<Integer, String> synchedMap = Collections.synchronizedMap(map); 

Common Mistakes in Collections

Source :

Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.

public class MyCustomKey implements Comparable
    private int value;
    public MyCustomKey(int value)
       this.value = value;

    public int compareTo (MyCustomKey key)
       int comparison = 0;           

       // Note:
       // Return -1 if this.value < key.value
       // Return  0 if this.value = key.value
       // Return  1 if this.value > key.value           

       return (comparison);

A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey()); may not give you what you were expecting. So it is always advised to override the hashCode() if objects of that class is being used as a key.

public class MyCustomKey implements Comparable
    private int value;
    public MyCustomKey(int value)
    public int compareTo (MyCustomKey key)

    public int hashCode()
        // Note:
        // If two objects are equal then their corresponding hashCode ()
        // should return the same value too.
        return (this.value * 199);

When you are using your custom object as a key to a HashMap, make sure you do the following

1) implement Comparable
2) override compareTo method, and give its implementation
3) override hashCode and equals method, and give their implementation.
4) Always, make your key object as immutable, so that it is not changed after you add it to a HashMap as key.

Sunday, February 13, 2011

Struts2 vs. Struts1

1.  Servlet Dependency:
Actions in Struts1 have dependencies on the servlet API since theHttpServletRequest and HttpServletResponse objects are passed to the execute method when an Action is invoked but in case of Struts 2, Actions are not container dependent because they are made simple POJOs. In struts 2, the servlet contexts are represented as simple Maps which allows actions to be tested in isolation. Struts 2 Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly

Struts 2 Iterator tag example

Source :

Struts 2 Iterator tag example

Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator. In this tutorials, you will create a list variable, use Iterator tag to loop over it and get the iterator status with IteratorStatus.

1. Action
An Action class with a List property , which contains variety of delicious “KFC combo meals”.

package com.mkyong.common.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class IteratorKFCAction extends ActionSupport{
 private List<String> comboMeals;
 public Listlt;String> getComboMeals() {
  return comboMeals;
 public void setComboMeals(List<String> comboMeals) {
  this.comboMeals = comboMeals;
 public String execute() {
  comboMeals = new ArrayList<String>();
  comboMeals.add("Snack Plate");
  comboMeals.add("Dinner Plate");
  comboMeals.add("Colonel Chicken Rice Combo");
  comboMeals.add("Colonel Burger");
  comboMeals.add("O.R. Fillet Burger");
  comboMeals.add("Zinger Burger");
  return SUCCESS;

Friday, February 4, 2011

Column names in hbm.xml

When i added a field in my pojo class, with name xMoveCompatibleItem and I used eclipse's inbuilt feature to generate getter and setter methods

Eclipse generated methods with the following names:

Note that X and M are both capital and consecutive, which was creating a problem for weblogic. It was not able to find the getter method for that property.

Do take care while adding a property, if the second letter of the property name is in CAPITAL