这是什么:[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
What is this: [Ljava.lang.Object;?
提问by Landon Kuhn
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.
当我调用toString
从函数调用中收到的对象时,我得到了这个。我知道这个字符串中编码了对象的类型,但我不知道如何读取它。
What is this type of encoding called?
这种类型的编码称为什么?
采纳答案by polygenelubricants
[Ljava.lang.Object;
is the name for Object[].class
, the java.lang.Class
representing the class of array of Object
.
[Ljava.lang.Object;
是 的名称Object[].class
,java.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 orvoid
.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 String
in this format is because arrays do not @Override
the method inherited from Object
, which is specified as follows:
toString()
数组上的方法之所以String
以这种格式返回,是因为数组没有@Override
继承自 的方法Object
,具体如下:
The
toString
method for classObject
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
classObject
的方法返回一个字符串,该字符串由对象是其实例的类的名称、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) @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 reflectingon 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).
注意:您不能依赖toString()
任何任意对象的 来遵循上述规范,因为它们可以(并且通常会这样做)@Override
返回其他内容。检查任意对象类型的更可靠方法是调用getClass()
它(final
从 继承的方法Object
),然后反射返回的Class
对象。但是,理想情况下,API 应该设计为不需要反射(参见Effective Java 2nd Edition,Item 53:Prefer interfaces to reflect)。
On a more "useful" toString
for arrays
toString
对数组更“有用”
java.util.Arrays
provides toString
overloads for primitive arrays and Object[]
. There is also deepToString
that you may want to use for nested arrays.
java.util.Arrays
toString
为原始数组和提供重载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.equals
and Arrays.deepEquals
that perform array equality comparison by their elements, among many other array-related utility methods.
除了许多其他与数组相关的实用方法之外,还有Arrays.equals
andArrays.deepEquals
通过它们的元素执行数组相等性比较。
Related questions
相关问题
- Java Arrays.equals() returns false for two dimensional arrays.-- in-depth coverage