这是什么:[Ljava.lang.Object;? [英] What is this: [Ljava.lang.Object;?

查看:32
本文介绍了这是什么:[Ljava.lang.Object;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get this when I call toString on 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.

这种类型的编码叫什么?

What is this type of encoding called?

推荐答案

[Ljava.lang.Object;Object[].class 的名称,java.lang.Class表示Object数组的类.

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

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

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

如果此类对象表示的引用类型不是数组类型,则返回类的二进制名称,如 Java 语言规范 (§13.1).

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).

如果这个类对象代表一个原始类型或者void,那么返回的名字就是原始类型对应的Java语言关键字或者void.

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;

您是该列表中的最后一个.以下是一些示例:

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

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

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

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

The toString method for class Object returns 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())

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

Note: you can not rely on the toString() of any arbitrary object to follow the above specification, since they can (and usually do) @Override it to return something else. The more reliable way of inspecting the type of an arbitrary object is to invoke getClass() on it (a final method inherited from Object) and then reflecting on the returned Class object. 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).

java.util.Arrays 为原始数组和 Object[] 提供了 toString 重载.还有 deepToString 您可能想用于嵌套数组.

java.util.Arrays provides toString overloads for primitive arrays and Object[]. There is also deepToString that you may want to use for nested arrays.

以下是一些示例:

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]]

还有 Arrays.equalsArrays.deepEquals 通过它们的元素执行数组相等性比较,以及许多其他与数组相关的实用方法.

There are also Arrays.equals and Arrays.deepEquals that perform array equality comparison by their elements, among many other array-related utility methods.

  • Java Arrays.equals() returns false for two dimensional arrays. -- in-depth coverage

这篇关于这是什么:[Ljava.lang.Object;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆