Creating an instance for Class?

2019-04-28 14:44发布

问题:

Looking at source code of Integer class, just stumble at this below line

Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

And getPrimitiveClass is a native method.

 static native Class getPrimitiveClass(String name);

Why it became a native method ? really want to know.

How one can create an instance for Class ?? Does that differs with normal way of creating instance for ex : Ex e = new Ex() ?

回答1:

The comment above the method definition says:

/*
 * Return the Virtual Machine's Class object for the named
 * primitive type.
 */
 static native Class getPrimitiveClass(String name);

Since the (at least, Sun's) Virtual Machine is implemented in C, then I would assume that this is the reason for the method being native.