In Java, do primitive types and arrays have a containing package?
在Java中,原始类型和数组是否包含包含?
Probably not, but just want to be certain.
可能不是,但只是想确定。
4 个解决方案
#1
17
Simple Answer
Let's test:
public static void main(final String[] args){
System.out.println(long.class.getPackage());
System.out.println(Object[].class.getPackage());
}
Output:
null
null
No they don't :-)
不,他们没有:-)
Primitive Types
Primitive classes are special constructs that don't have a package. For reference, see the source of Long.TYPE
, the alias for long.class
:
原始类是没有包的特殊构造。有关参考,请参阅Long.TYPE的来源,long.cTYPE是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);
and casted to Class<Long>
(in order to enable auto-boxing, I guess)
并转换为Class
Wrapper Types and their Primitive Types
BTW: every wrapper class has a static final field called TYPE
that maps to the corresponding primitive class, as the following code shows:
顺便说一句:每个包装类都有一个名为TYPE的静态final字段,它映射到相应的基本类,如下面的代码所示:
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());
}
}
Output:
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包装器类型:java.lang.Byte,原始类型:byte包装器类型:java.lang.Long,基本类型:long包装器类型:java.lang.Integer,基本类型:int包装器类型:java.lang.Short,基本类型:short Wrapper类型:java.lang.Boolean,primitive type:boolean Wrapper类型:java.lang.Double,primitive类型:double
Array Types
Arrays can be created through Array.newInstance(type, length)
, which internally calls this method:
可以通过Array.newInstance(type,length)创建数组,它在内部调用此方法:
private static native Object newArray(Class componentType, int length)
throws NegativeArraySizeException;
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)
所以,类是由本机代码创建的特殊构造(并且它们没有包,否则你可以在某处找到它们)
#2
4
No they don't have,as they aren't class.
不,他们没有,因为他们不是上课。
Primitive : A primitive type is predefined by the language and is named by a reserved keyword.
基元:基本类型由语言预定义,并由保留关键字命名。
array : An array is a container object that holds a fixed number of values of a single type.
array:数组是一个容器对象,它包含固定数量的单个类型的值。
#3
1
No, but there are objects to wrap primitive data types in the java.lang
package.
不,但是有一些对象可以在java.lang包中包装原始数据类型。
http://download.oracle.com/javase/6/docs/api/java/lang/package-summary.html
#4
1
No since they are language constructs and not classes per-se.
不,因为它们是语言结构而不是类本身。
You can however have a class representing a primitive type in the wrapper for instance:
但是,您可以在包装器中有一个表示基本类型的类,例如:
Integer.TYPE et al. useful for reflection.
Integer.TYPE等。对反射很有用。
But you'll see that still those don't have a package ie.
但是你会看到那些没有包的东西,即。
Integer.TYPE.getPackage()
Returns null
#1
17
Simple Answer
Let's test:
public static void main(final String[] args){
System.out.println(long.class.getPackage());
System.out.println(Object[].class.getPackage());
}
Output:
null
null
No they don't :-)
不,他们没有:-)
Primitive Types
Primitive classes are special constructs that don't have a package. For reference, see the source of Long.TYPE
, the alias for long.class
:
原始类是没有包的特殊构造。有关参考,请参阅Long.TYPE的来源,long.cTYPE是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);
and casted to Class<Long>
(in order to enable auto-boxing, I guess)
并转换为Class
Wrapper Types and their Primitive Types
BTW: every wrapper class has a static final field called TYPE
that maps to the corresponding primitive class, as the following code shows:
顺便说一句:每个包装类都有一个名为TYPE的静态final字段,它映射到相应的基本类,如下面的代码所示:
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());
}
}
Output:
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包装器类型:java.lang.Byte,原始类型:byte包装器类型:java.lang.Long,基本类型:long包装器类型:java.lang.Integer,基本类型:int包装器类型:java.lang.Short,基本类型:short Wrapper类型:java.lang.Boolean,primitive type:boolean Wrapper类型:java.lang.Double,primitive类型:double
Array Types
Arrays can be created through Array.newInstance(type, length)
, which internally calls this method:
可以通过Array.newInstance(type,length)创建数组,它在内部调用此方法:
private static native Object newArray(Class componentType, int length)
throws NegativeArraySizeException;
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)
所以,类是由本机代码创建的特殊构造(并且它们没有包,否则你可以在某处找到它们)
#2
4
No they don't have,as they aren't class.
不,他们没有,因为他们不是上课。
Primitive : A primitive type is predefined by the language and is named by a reserved keyword.
基元:基本类型由语言预定义,并由保留关键字命名。
array : An array is a container object that holds a fixed number of values of a single type.
array:数组是一个容器对象,它包含固定数量的单个类型的值。
#3
1
No, but there are objects to wrap primitive data types in the java.lang
package.
不,但是有一些对象可以在java.lang包中包装原始数据类型。
http://download.oracle.com/javase/6/docs/api/java/lang/package-summary.html
#4
1
No since they are language constructs and not classes per-se.
不,因为它们是语言结构而不是类本身。
You can however have a class representing a primitive type in the wrapper for instance:
但是,您可以在包装器中有一个表示基本类型的类,例如:
Integer.TYPE et al. useful for reflection.
Integer.TYPE等。对反射很有用。
But you'll see that still those don't have a package ie.
但是你会看到那些没有包的东西,即。
Integer.TYPE.getPackage()
Returns null