Monday, November 4, 2013

Can you spot a NPE (Null Pointer Exception)?

What is the most common exception that java developer are experiencing? The straight forward answer is NPE. Here I’m not going to discuss all the possibilities of NPE but one freak possibility of getting NPE, you may miss while debugging the code.

Did you spot the NPE possibility ?

This code seems to be fine, there are null checks, no chance of getting NPE at for each loop ( it throws NPE if the list is null ). So where is the NPE possible loophole? Value of get(0) may be null. List is not like other collections, it can contain null values. So two things to remember.
  1. Do a null check before call trim method whenever there is a chance of getting null values as a String.
  2. List is a sequence of objects so there is no limitation of having null as an element of the list, Set also can contain at most one null element. So beware of such hidden null values.

Sunday, November 3, 2013

Till we move to Diamond Operator

Have you ever come across a scenario, where a single line of code may not be enough to declare a generalized collection? If you don’t remember, just refresh your memory with follow simple code example. It is pretty obvious, the redundant generalization cause the lengthy collection declaration. There is no wrong here but the language should do the RHS for us. Because the future of the code is depends on the cleanness of it.

How about follow code sample, which one is clearer to read or understand.

How we do it?
Before java 7, there is no automatic type inference feature or an operator bundled with java. In java 7 there is a way, it calls the diamond operator.

Functionality of Diamond operator. <>
Following code line has same effect as the above two map declarations.

Way for pre java 7 developers.
Generic static factory method is their survivor.

Tuesday, September 4, 2012

Genarics vs it's byte code

Lets begin this discussion with some code samples

sample 1 class with out generics
sample 2 class with generics

As every one can see sample one class does not generalize its map but in sample two it is generalized.Now we compile both classes and we get two byte codes , Lets name them as byte code 1 and byte code 2 .

Are those two byte codes are same or not ?

If you look blindly you may answer that question with no , because every one can see that the content of two classes are different, so there can not be same byte code for both sample one and sample two classes . But truth is they are same.

How they are same and why it is same ?

Its better to know why it is same before we hands on how it is same . Simple answer for why it is same is , to ensure the backward compatibility of the code.Java introduce Generics with java 5 version , if we change infrastructure of java byte code with version 5 those code may not be compatible with previous versions . In simple your pre java 5 classes can not communicate with your java 5 object if their byte code infrastructure is different.

Now we can hands on how it is done.When java compile the source code it ensure the type safety by using generics after that compiler erase all the generics using eraser utility and then compile the source code now there is no generics in the byte code but code is type safe to more information on how this work go to follow this java documentation .