Wednesday, August 18, 2010

What compiler is doing with generics, new for loop, enum, autoboxing etc?

Ever wondered how erasure, enum, new for loop, auto-boxing/unboxing etc. is wroking? Java compiler plays big role to achive all these. I would say all these are compiler phenomenon. decompiled class files helped me to understand all these but recently I came across a javac flag which gives translated/converted java files -

javac -XD-printflat -d some_output_location FileToTest.java

Here -printflat tells compiler to generate flat file and -d some_output_location is place out files will be placed.


To give you an idea what it does we will do a small test. Here is the piece of java code which shows how autoboxing works -

Integer i = 15;

and here is the outcome of this command (just converted code)-

Integer i = Integer.valueOf( 15 );

Although .java file does not change but bytecode is generated for translated code.