Tuesday, June 23, 2009

Case constants and compile time constants.

In Java, case constants in switch statements have to be compile time constants and ofcourse be byte, short, char, int or enum. Compile time constants' values are known at compile time. They are the static finals like 

static final int ME = 1;

static final byte BE = 1;

static final int US = ME*2;

whose values are known or can be computed at compile time. 

Not all static finals can be computed or known at compile time. For example the following cannot be computed at compile time:

static final int CURRENT_TIME = (int)System.currentTimeMillis()*24*60*60;

So it cannot be used as a case constant.

Now how about wrappers? Can the following be used as a case constant?

static final Integer CONSTANT = new Integer(1);

Wrappers are immutable and since it is declared as static final, the variable CONSTANT  is actually a constant, and since it can be auto unboxed, it can be used as a switch constant, right? Wrong! It turns out that it is not a compile time constant. Auto unboxing converts the Integer wrapper to a primitive int as follows:


So the value cannot be computed at compile time. In effect due to this conversion that needs to happen (eventhough it happens implicitly), wrappers cannot be compile time constants.

Tuesday, June 16, 2009

Simply hashcode

In Java, here are the key things to remember about hashcode:
  • If you override equals method also override hashcode method. The default implementation of hashcode returns the 32-bit internal JVM address of the object. So if you do not override hashcode, the object cannot be used in data structures that use hashing mechanism. 
  • If two objects are equal, their hashcodes must be equal. However the reverse is not true i.e. if the hashcodes are equal, the objects may or may not be equal/same.
  • If hashcodes of 2 objects are NOT same, the two objects are NOT equal.
  • If both equals and hashcode are overridden, they must use the same set of fields. Equals may use more fields than hashcode uses but it must use all the fields that hashcode uses. The fields must be final i.e. immutable. If the fields are changeable, you may not get accurate results.
  • Hashcode DOES NOT provide a unique identifier for an object.
  • Hashcodes can be negative.

Saturday, June 13, 2009

Restoring files in Eclipse

Restoring deleted files
Restoring deleted files in Eclipse IDE is very simple, just do the following:
- Select the project in which o restore the file.
- From the pop-up menu select 'Restore from local history' option. (In case of mac os, hold the ctrl key and click on the project to invoke the pop-up)

- Select the file to restore. Select the edition of the file to restore.

- Click on restore.

Restoring a different version of the file
- Select the file to restore.
- From the pop-up menu select 'show in'. Select 'history'.

- It opens the history perspective.
- Select the version to be restored. To compare the version just choose 'Compare current with local' from the pop-up menu.

- Double click on the version to restore it.

Wednesday, June 10, 2009

Graphical Resume

Very creative resume for creative jobs! And here is the explanation. Enjoy.