这是什么:[Ljava.lang.Object ;? [英] What is this: [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语言规范(如果此类对象表示原始类型或 If this class object represents a primitive type or 如果此类对象表示一类数组,则名称的内部形式由元素类型的名称组成,后跟一个或多个表示数组嵌套深度的 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
您是列表中的最后一个.以下是一些示例: Yours is the last on that list. Here are some examples: 数组上的 The reason why the The
注意 :您不能依赖任何任意对象的 Note: you can not rely on the 以下是一些示例: 还有 There are also 这篇关于这是什么:[Ljava.lang.Object ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!void
,则返回的名称是与原始类型或void
相对应的Java语言关键字. void
, then the name returned is the Java language keyword corresponding to the primitive type or void
. '['
字符.
元素类型名称的编码如下:'['
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;
// 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
继承的方法,其指定如下: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:
toString
类Object
的方法返回一个字符串,该字符串由对象作为实例的类的名称,符号字符"@"和对象的哈希码的无符号十六进制表示组成.换句话说,此方法返回的字符串等于:
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
返回其他东西.检查任意对象类型的更可靠方法是调用反射在返回的Class
对象上.不过,理想情况下,API的设计应该使得不需要反射(请参阅 Effective Java 2nd Edition,第53项:优先使用反射接口).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.equals
和Arrays.deepEquals
可以通过其元素执行数组相等性比较,还有许多其他与数组相关的实用程序方法.Arrays.equals
and Arrays.deepEquals
that perform array equality comparison by their elements, among many other array-related utility methods.