在Java中,做到原始类型和数组含有包? [英] In Java, do primitive types and arrays have a containing package?
问题描述
在Java中,做到原始类型和数组含有包?
In Java, do primitive types and arrays have a containing package?
大概不会,但只是想肯定。
Probably not, but just want to be certain.
推荐答案
让我们来测试:
public static void main(final String[] args){
System.out.println(long.class.getPackage());
System.out.println(Object[].class.getPackage());
}
输出:
空结果
空
null
null
没有他们不这样做: - )
No they don't :-)
原始类是没有包特殊构造。仅供参考,请参阅源<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Long.html#TYPE\"><$c$c>Long.TYPE$c$c>,别名为 long.class
:
Primitive classes are special constructs that don't have a package. For reference, see the source of Long.TYPE
, the alias for long.class
:
/**
* The <code>Class</code> instance representing the primitive type
* <code>long</code>.
*
* @since JDK1.1
*/
public static final Class<Long> TYPE =
(Class<Long>) Class.getPrimitiveClass("long");
正如你所看到的,一个原始的类是通过包私人和原生机制加载:
As you can see, a primitive class is loaded through a package-private and native mechanism:
static native Class getPrimitiveClass(String name);
和铸造到类&LT;龙&GT;
(为了使自动装箱,我猜)
and casted to Class<Long>
(in order to enable auto-boxing, I guess)
BTW:每个包装类都有一个称为静态最终字段键入
映射到相应的原始类,如下面的code所示:
BTW: every wrapper class has a static final field called TYPE
that maps to the corresponding primitive class, as the following code shows:
private static Class<?> getPrimitiveClass(final Class<?> wrapperClass){
try{
final Field field = wrapperClass.getDeclaredField("TYPE");
final int modifiers = field.getModifiers();
if(Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
&& Modifier.isFinal(modifiers)
&& Class.class.equals(field.getType())){
return (Class<?>) field.get(null);
} else{
throw new IllegalArgumentException("This is not a wrapper class: "
+ wrapperClass);
}
} catch(final NoSuchFieldException e){
throw new IllegalArgumentException("This is not a wrapper class:"
+ wrapperClass + ", field TYPE doesn't exists.", e);
} catch(final IllegalAccessException e){
throw new IllegalArgumentException("This is not a wrapper class:"
+ wrapperClass + ", field TYPE can't be accessed.", e);
}
}
public static void main(final String[] args){
final List<Class<?>> wrappers =
Arrays.<Class<?>> asList(
Byte.class, Long.class, Integer.class,
Short.class, Boolean.class, Double.class
// etc.
);
for(final Class<?> clazz : wrappers){
System.out.println("Wrapper type: " + clazz.getName()
+ ", primitive type: "
+ getPrimitiveClass(clazz).getCanonicalName());
}
}
输出:
包装类型:java.lang.Byte中,基本类型:字节结果
包装类型:java.lang.Long中,基本类型:长期结果
包装类型:java.lang.Integer中,基本类型:INT结果
包装类型:java.lang.Short中,基本类型:短期结果
包装类型:java.lang.Boolean中,基本类型:布尔结果
包装类型:java.lang.Double中,基本类型:双
Wrapper type: java.lang.Byte, primitive type: byte
Wrapper type: java.lang.Long, primitive type: long
Wrapper type: java.lang.Integer, primitive type: int
Wrapper type: java.lang.Short, primitive type: short
Wrapper type: java.lang.Boolean, primitive type: boolean
Wrapper type: java.lang.Double, primitive type: double
阵列可以通过<一个创建href=\"http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html#newInstance%28java.lang.Class%2C%20int%29\"><$c$c>Array.newInstance(type,长度) ,该内部调用此方法:
Array Types
Arrays can be created through Array.newInstance(type, length)
, which internally calls this method:
private static native Object newArray(Class componentType, int length)
throws NegativeArraySizeException;
如此反复,这些类特殊的构造母语code创建(他们没有一个包,否则你可能会在某个地方找到他们)
so again, the classes are special constructs created by native code (and they don't have a package, or else you could find them somewhere)
这篇关于在Java中,做到原始类型和数组含有包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!