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

查看:317
本文介绍了java:这是什么:[Ljava.lang.Object ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从函数调用接收到的对象上调用 toString 时,我得到这个。我知道对象的类型是编码在这个字符串,但我不知道如何读取它。这种类型的编码叫做什么?

解决方案

[Ljava.lang.Object; Object []。的名称, java.lang.Class 表示 Object



命名方案记录在 Class.getName()


如果此类对象表示不是数组类型的引用类型,则返回类的二进制名称,如Java语言规范§13.1)。



如果此类对象表示原始类型或 void ,则返回的名称是与原始类型对应的Java语言关键字, void



如果此类对象表示一个数组类,那么该名称的内部形式由一个或多个'['字符表示数组嵌套的深度。
元素类型名称的编码如下:

 元素类型编码
boolean Z
byte B
char C
double D
float F
int I
long J
short S
类或接口Lclassname;


以下是一些示例:

  // xxxxx vary 
System.out.println(new int [0] [0] [7]); // [[[@ @ xxxxx
System.out.println(new String [4] [2]); // [[Ljava.lang.String; @xxxxx
System.out.println(new boolean [256]); / toString() code>方法返回 String 在这种格式是因为数组不 @Override 对象,其指定如下:


toString 对象 返回一个字符串,其中的对象是一个实例,@符号,和对象的散列码的无符号十六进制表示。换句话说,此方法返回一个等于以下值的字符串:

  getClass()。getName()+'@' + Integer.toHexString(hashCode())


em> :你不能依赖任何任意对象的 toString()遵循上面的规范, do) @Override 它返回别的东西。检查任意对象类型的更可靠方法是调用 getClass() (一个 final Object ),然后在所返回的 Class 对象上反映。理想情况下,API应该设计为不需要反射(参见<有效Java第2版,第53项:优选反射界面)。






在一个更有用的 toString 数组



java.util .Arrays 为原始数组和 Object [] 提供 toString 重载。还有 deepToString ,你可能想要用于嵌套数组。



这里有一些例子:

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


$ b b

还有 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天全站免登陆