具有数组类型的 getClass 方法 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6867131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:38:44  来源:igfitidea点击:

getClass method Java with array types

javaarraysclassreturn-value

提问by Dustin Jensen

So I've run into something odd and I don't know what it's called so I'm having trouble finding out information about it, hence my question here.

所以我遇到了一些奇怪的事情,我不知道它叫什么,所以我很难找到有关它的信息,因此我的问题在这里。

I've run into an issue where if you create an array of any type and call getClass on this array in Java you will get an odd return. I am wondering why you get this specific return and what it means.

我遇到了一个问题,如果您创建一个任何类型的数组并在 Java 中对该数组调用 getClass,您将得到一个奇怪的回报。我想知道为什么你会得到这个特定的回报以及它意味着什么。

Code example is as follows:

代码示例如下:

byte[] me = new byte[1];
int[] me2 = new int[1];
double[] me3 = new double[1];
float[] me4 = new float[1];
String[] me5 = new String[1];
Integer[] me6 = new Integer[1];

System.out.println(me.getClass());                  
System.out.println(me2.getClass());                 
System.out.println(me3.getClass());                 
System.out.println(me4.getClass());                 
System.out.println(me5.getClass());
System.out.println(me6.getClass());

and the output is:

输出是:

 class [B
 class [I
 class [D
 class [F
 class [Ljava.lang.String;
 class [Ljava.lang.Integer;

回答by emory

The toStringmethod of Class invokes the getNamemethod of Class which

Class的toString方法调用了 Class 的getName方法

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by The Java? Language Specification.

If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void.

If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. The encoding of element type names is as follows:

 Element Type           Encoding
 boolean                Z
 byte                   B
 char                   C
 class or interface     Lclassname;
 double                 D
 float                  F
 int                    I
 long                   J
 short                  S 

The class or interface name classnameis the binary name of the class specified above.

Examples:

 String.class.getName()
     returns "java.lang.String"
 byte.class.getName()
     returns "byte"
 (new Object[3]).getClass().getName()
     returns "[Ljava.lang.Object;"
 (new int[3][4][5][6][7][8][9]).getClass().getName()
     returns "[[[[[[[I"

以 String 形式返回由此 Class 对象表示的实体(类、接口、数组类、原始类型或 void)的名称。如果此类对象表示不是数组类型的引用类型,则返回该类的二进制名称,如 The Java? 语言规范。

如果这个类对象表示一个原始类型或void,则返回的名称是一个String,等于原始类型或void对应的Java语言关键字。

如果此类对象表示一类数组,则名称的内部形式由元素类型的名称组成,前面有一个或多个代表数组嵌套深度的“[”字符。元素类型名称的编码如下:

 Element Type           Encoding
 boolean                Z
 byte                   B
 char                   C
 class or interface     Lclassname;
 double                 D
 float                  F
 int                    I
 long                   J
 short                  S 

类或接口名称classname是上面指定的类的二进制名称。

例子:

 String.class.getName()
     returns "java.lang.String"
 byte.class.getName()
     returns "byte"
 (new Object[3]).getClass().getName()
     returns "[Ljava.lang.Object;"
 (new int[3][4][5][6][7][8][9]).getClass().getName()
     returns "[[[[[[[I"

回答by irreputable

It's just some stupid naming convention. Would been much better if they are more humanly readable: class byte[], class java.lang.Integert[][]

这只是一些愚蠢的命名约定。如果它们更具人类可读性会更好:class byte[]class java.lang.Integert[][]

回答by Ted Hopp

The [at the start of the class name should be read as "array of ...", with "..." being what follows the [; the conventions for what follows are spelled out in the documentation for Class.getName()that @emory cited and linked to.

[在类名称的开头应该读作“...的阵列”,用“......”是什么如下[; 以下内容的约定在该Class.getName()@emory 引用和链接的文档中详细说明。

Section 4.3.1 of the Java Language Specificationstarts: "An object is a class instance or an array." This suggests that arrays are not class instances. (Perhaps this is what @TigerTrussell was getting at in his answer.) However, Section 10.8starts: "Every array has an associated Class object, shared with all other arrays with the same component type. The direct superclass of an array type is Object." This suggests that arrays are, if not true class instances, pretty darn close.

Java 语言规范的第 4.3.1 节开始:“对象是类实例或数组。” 这表明数组不是类实例。(也许这就是@TigerTrussell 在他的回答中得到的。)然而,第 10.8 节开始:“每个数组都有一个关联的 Class 对象,与具有相同组件类型的所有其他数组共享。数组类型的直接超类是 Object .” 这表明数组即使不是真正的类实例,也非常接近。

Unfortunately, the syntax rules for Java prohibit inheriting from an array type:

不幸的是,Java 的语法规则禁止从数组类型继承:

public class MyFancyArray extends int[] { // ILLEGAL
   // ...
}

Note that this is a syntax constraint; there are no conceptual or semantic barriers (and it's allowed in many other OO languages).

请注意,这是一个语法约束;没有概念或语义障碍(并且在许多其他面向对象语言中是允许的)。

回答by Jim Garrison

Those are the names of the underlying type object. The [indicates it's an array, and the following letter indicates the array type. B=byte, I=integer, D=double, etc. "L" is for class type members as you can see.

这些是基础类型对象的名称。在[表明它是一个数组,以下字母表示数组类型。B=byte、I=integer、D=double 等。正如您所见,“L”用于类类型成员。

回答by TigerTrussell

Arrays don't have classes, they are simply data structures. The only thing having a class would be something extended from Object which is included in the array.

数组没有类,它们只是数据结构。唯一具有类的东西是从包含在数组中的 Object 扩展的东西。