java的:这是什么:[Ljava.lang.Object ;? [英] java: what is this: [Ljava.lang.Object;?
问题描述
我得到这个,当我打电话的toString
我从一个函数调用接收的对象。我知道这个对象的类型是连接在此字符串$ C $的CD,但我不知道如何读它。这是什么类型的编码叫什么名字?
[Ljava.lang.Object;
是名称的对象[]的.class
的<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Class.html\"><$c$c>java.lang.Class$c$c>再presenting类对象的数组
。
命名方案在<一个记录href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getName%28%29\"><$c$c>Class.getName()$c$c>:
如果该类对象重新presents引用类型不是数组类型,则返回该类的二进制名称,由Java语言规范(指定的§13.1)。
如果该类对象重新presents一个基本类型或
无效
,则返回的名称是对应于基本类型或<$ C $ Java语言的关键字C>无效。
如果该类对象重新presents一类阵列,那么名称的内部形式包括由一个或多个
'['pceded元素类型$ P $的名称
字符重新presenting数组嵌套的深度。
元素类型名的编码如下:元素类型编码
布尔ž
BYTE B
焦炭ç
双D
浮˚F
INT I
龙J
短小号
类或接口Lclassname;
块引用>下面是一些例子:
// xxxxx的变化
的System.out.println(新INT [0] [0] [7]); // [[[I @ XXXXX
的System.out.println(新字符串[4] [2]); // [Ljava.lang.String; @xxxxx
的System.out.println(新布尔[256]); // [Z @ XXXXX之所以对数组返回
的toString()
方法字符串
这种格式是因为数组不@覆盖
>对象从
的<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29\"><$c$c>toString$c$c>类
对象
返回一个包含其中的对象是一个实例,该符号字符'@'的类名的字符串,该方法该对象的哈希code的无符号十六进制重新presentation。换句话说,该方法返回字符串等于的值:的getClass()。的getName()+'@'+ Integer.toHexString(哈希code())
块引用>的 注意的:你不能依靠任意对象的
的toString()
遵循上述规范,因为他们可以(通常)@覆盖
它返回别的东西。检查类型的任意对象的更可靠的方法是调用<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#getClass%28%29\"><$c$c>getClass()$c$c> //java.sun:它,然后的对象继承了最后
法) .COM /开发商/ technicalArticles / ALT /反射/ index.html的>反映返回类
对象。理想的情况下,虽然,API应该已经被设计成反射是没有必要的(见的有效的Java第二版,第53条:preFER接口反射的)。在一个更有用的
的toString
数组<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html\"><$c$c>java.util.Arrays$c$c>提供
的toString
重载基本数组和对象[]
。还有deepToString
,你可能要使用嵌套数组。下面是一些例子:
INT [] = NUMS {1,2,3}; 的System.out.println(NUMS);
// [I @ XXXXX 的System.out.println(Arrays.toString(NUMS));
// [1,2,3] INT [] [] =表{
{1,},
{2,3,},
{4,5,6,},
}; 的System.out.println(Arrays.toString(表));
// [I @ XXXXX,[I @ YYYYY,[I @ ZZZZZ] 的System.out.println(Arrays.deepToString(表));
// [[1],[2,3],[4,5,6]也有
满足Arrays.equals
和Arrays.deepEquals
,通过它们的元素进行排列相等比较,在许多其他阵列相关的实用程序方法。相关问题
- Java满足Arrays.equals()返回二维数组假 - 深入报道
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;
is the name forObject[].class
, thejava.lang.Class
representing the class of array ofObject
.The naming scheme is documented in
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;
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 returnsString
in this format is because arrays do not@Override
the method inherited fromObject
, which is specified as follows: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())
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 invokegetClass()
on it (afinal
method inherited fromObject
) and then reflecting on the returnedClass
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).
On a more "useful"
toString
for arrays
java.util.Arrays
providestoString
overloads for primitive arrays andObject[]
. There is alsodeepToString
that you may want to use for nested arrays.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
andArrays.deepEquals
that perform array equality comparison by their elements, among many other array-related utility methods.Related questions
- Java Arrays.equals() returns false for two dimensional arrays. -- in-depth coverage
这篇关于java的:这是什么:[Ljava.lang.Object ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!