Java is not purely Object Oriented - what does thi

2019-08-25 09:29发布

问题:

When I look at the javadocs of Class.java it says:

/*The primitive Java types ({@code boolean},
 * {@code byte}, {@code char}, {@code short},
 * {@code int}, {@code long}, {@code float}, and
 * {@code double}), and the keyword {@code void} are also
 * represented as {@code Class} objects.
*/

I had always heard that primitive types in Java are not classes/objects hence Java isn't purely Object Oriented. But the above JavaDoc confuses me.

回答1:

Don't get confused by the term "represented" and mess up "type" and "value". The representations are created by the Java Virtual Machine, they are not instantiated for every primitive value (this would be autoboxing, though some instances are cached):

There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double.

These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isPrimitive%28%29