When I have a lot of type checks in Java like this:
if (clazz.equals(Byte.class) <dosomething>
if (clazz.equals(Integer.class) <dosomething>
...
Does the JVM load all these class (Byte, Integer?) And if it does, is there another way to do class type checking without loading a bunch of classes I might not even need?
Yes, using
.classwill load the class, but don’t worry about these — everything injava.langis going to already be loaded before your program even runs, because these classes are all either used by the JVM itself, or used by other API classes that are pre-loaded.If you wanted to check for a class without loading the class, you could do something like
but that would be rather fragile; I’d avoid it unless there was a really good reason.
Anyway, start Java with
-verbose:classsometime to see all the classes that are pre-loaded for you!