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

查看:1002
本文介绍了java的:这是什么:[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再presenting类对象的数组

命名方案在<一个记录href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getName%28%29\"><$c$c>Class.getName():


  

如果该类对象重新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对象 返回一个包含其中的对象是一个实例,该符号字符'@'的类名的字符串,该方法该对象的哈希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() //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提供的toString 重载基本数组和对象[] 。还有 deepToString ,你可能要使用嵌套数组。

下面是一些例子:

  INT [] = NUM​​S {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 ,通过它们的元素进行排列相等比较,在许多其他阵列相关的实用程序方法。

相关问题

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 for Object[].class, the java.lang.Class representing the class of array of Object.

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

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:

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

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


On a more "useful" toString for arrays

java.util.Arrays provides toString overloads for primitive arrays and Object[]. There is also deepToString 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 and Arrays.deepEquals that perform array equality comparison by their elements, among many other array-related utility methods.

Related questions

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

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