I've been doing Java for a long time and I feel pretty confident
that I know just about everything there is to know about the language
and JVM. Obviously that is an overstatement, but I do try to make
sure I always know more than the guy next to me. But, I sure feel
dumb today.
Class literals have been around since Java 1.1. Everyone knows
you can use String.class> to get the Class object for
java.lang.String. Under the covers the compiler caches a the results
of Class.forName("java.lang.String") in your class, and all is well.
When I've needed a Class object to represent a primitive type like
int, I've always used Integer.TYPE. Somehow in the mix of things,
I never realized you could also use int.class to get the Class.
This compiles down to a reference to Integer.TYPE, so it's really just
a question of readability. It's no big deal, but I'm shocked I never
realized this was possible before.
But, my shame doesn't end there. What if you need a Class for an
array type? You have to call Class.forName("[I") and curse the
language designers, right? Well, that's what I thought until I found
out I could have simply used int[].class instead. Again, there's no
magic there. The same Class.forName() method is called. But, there's
no doubt which one makes for better code.
So, I hang my head in shame that somehow a basic language
construct that has been around forever has escaped me for so long.
For penance, I'll have to read the Java language specification to see
if I've overlooked any other ebarassingly simple language
features.