In Java, do primitive types and arrays have a containing package?

落爺英雄遲暮 提交于 2019-12-17 20:23:52

问题


In Java, do primitive types and arrays have a containing package?

Probably not, but just want to be certain.


回答1:


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:

/**
 * 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)

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:

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


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;

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:


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.




回答3:


No, but there are objects to wrap primitive data types in the java.lang package.

http://download.oracle.com/javase/6/docs/api/java/lang/package-summary.html




回答4:


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.

But you'll see that still those don't have a package ie.

Integer.TYPE.getPackage()

Returns null



来源:https://stackoverflow.com/questions/4737311/in-java-do-primitive-types-and-arrays-have-a-containing-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!