为什么原始类型具有“类”以及它如何用于使用?
谈到Java(7),你可以得到一个类这样的原始类:
Class classOfInt = int.class
对于每一个,你将获得一个名为基本类型的“类”:
int.class --> int byte.class --> byte double.class --> double ...
但是,您无法创建这些实例:
char.class.newInstance(); // throws 'InstantiationException'
它们的类似乎没有映射到相应的包装类( Integer
, Byte
等)。
那么为什么他们有“课程”,他们如何使用以及如何实施?
它们用于reflection。
Method round = Math.class.getMethod("round", double.class); System.out.println(Arrays.toString(round.getParameterTypes())); System.out.println(round.getReturnType() == long.class); Method exit = System.class.getMethod("exit", int.class); System.out.println(Arrays.toString(exit.getParameterTypes())); System.out.println(exit.getReturnType() == void.class);
版画
[double] true [int] true
他们是如何实施的?
它们内置于JVM,没有用于定义它们的类文件。