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

查看:78
本文介绍了这是什么:[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.

这种编码类型叫什么?

推荐答案

[Ljava.lang.Object;Object[].class的名称,

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

如果该类对象表示的引用类型不是数组类型,则返回该类的二进制名称,如Java语言规范(如果此类对象表示原始类型或void,则返回的名称是与原始类型或void相对应的Java语言关键字.

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的原因是因为数组不是@OverrideObject继承的方法,其指定如下:

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:

toStringObject 的方法返回一个字符串,该字符串由对象作为实例的类的名称,符号字符"@"和对象的哈希码的无符号十六进制表示组成.换句话说,此方法返回的字符串等于:

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返回其他东西.检查任意对象类型的更可靠方法是调用

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[]的重载.您还可能要使用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天全站免登陆