这是什么:[Ljava.lang.Object;?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3442090/
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-08-14 00:48:45  来源:igfitidea点击:

What is this: [Ljava.lang.Object;?

javaarraysclasstostring

提问by Landon Kuhn

I get this when I call toStringon an object I received from a function call. I know the type of the object is encoded in this string, but I don't know how to read it.

当我调用toString从函数调用中收到的对象时,我得到了这个。我知道这个字符串中编码了对象的类型,但我不知道如何读取它。

What is this type of encoding called?

这种类型的编码称为什么?

采纳答案by polygenelubricants

[Ljava.lang.Object;is the name for Object[].class, the java.lang.Classrepresenting the class of array of Object.

[Ljava.lang.Object;是 的名称Object[].classjava.lang.Class代表 的数组的类Object

The naming scheme is documented in Class.getName():

命名方案记录在Class.getName()

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 (§13.1).

If this class object represents a primitive type or void, then the name returned is 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
double              D
float               F
int                 I
long                J
short               S 
class or interface  Lclassname;

如果此类对象表示不是数组类型的引用类型,则返回该类的二进制名称,如 Java 语言规范(第13.1 节)所指定。

如果该类对象表示原始类型 or void,则返回的名称是原始类型 or 对应的 Java 语言关键字void

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

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

Yours is the last on that list. Here are some examples:

你是那个名单上的最后一个。这里有些例子:

// xxxxx varies
System.out.println(new int[0][0][7]); // [[[I@xxxxx
System.out.println(new String[4][2]); // [[Ljava.lang.String;@xxxxx
System.out.println(new boolean[256]); // [Z@xxxxx

The reason why the toString()method on arrays returns Stringin this format is because arrays do not @Overridethe method inherited from Object, which is specified as follows:

toString()数组上的方法之所以String以这种格式返回,是因为数组没有@Override继承自 的方法Object,具体如下:

The toStringmethod for class Objectreturns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

toStringclassObject方法返回一个字符串,该字符串由对象是其实例的类的名称、at 符号字符“@”和对象哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于以下值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Note: you can not rely on the toString()of any arbitrary object to follow the above specification, since they can (and usually do) @Overrideit to return something else. The more reliable way of inspecting the type of an arbitrary object is to invoke getClass()on it (a finalmethod inherited from Object) and then reflectingon the returned Classobject. Ideally, though, the API should've been designed such that reflection is not necessary (see Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection).

注意:您不能依赖toString()任何任意对象的 来遵循上述规范,因为它们可以(并且通常会这样做)@Override返回其他内容。检查任意对象类型的更可靠方法是调用getClass()它(final从 继承的方法Object),然后反射返回的Class对象。但是,理想情况下,API 应该设计为不需要反射(参见Effective Java 2nd Edition,Item 53:Prefer interfaces to reflect)。



On a more "useful" toStringfor arrays

toString对数组更“有用”

java.util.Arraysprovides toStringoverloads for primitive arrays and Object[]. There is also deepToStringthat you may want to use for nested arrays.

java.util.ArraystoString为原始数组和提供重载Object[]。还有deepToString,你可能要使用嵌套的数组。

Here are some examples:

这里有些例子:

int[] nums = { 1, 2, 3 };

System.out.println(nums);
// [I@xxxxx

System.out.println(Arrays.toString(nums));
// [1, 2, 3]

int[][] table = {
        { 1, },
        { 2, 3, },
        { 4, 5, 6, },
};

System.out.println(Arrays.toString(table));
// [[I@xxxxx, [I@yyyyy, [I@zzzzz]

System.out.println(Arrays.deepToString(table));
// [[1], [2, 3], [4, 5, 6]]

There are also Arrays.equalsand Arrays.deepEqualsthat perform array equality comparison by their elements, among many other array-related utility methods.

除了许多其他与数组相关的实用方法之外,还有Arrays.equalsandArrays.deepEquals通过它们的元素执行数组相等性比较。

Related questions

相关问题