gpt4 book ai didi

java - 具有数组类型的 getClass 方法 Java

转载 作者:太空狗 更新时间:2023-10-29 22:39:15 26 4
gpt4 key购买 nike

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

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

代码示例如下:

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());

输出是:

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

最佳答案

toString类的方法调用 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 classname is 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"

关于java - 具有数组类型的 getClass 方法 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com