具有数组类型的 getClass 方法 Java [英] getClass method Java with array types

查看:43
本文介绍了具有数组类型的 getClass 方法 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一些奇怪的事情,我不知道它叫什么,所以我很难找到有关它的信息,因此我的问题在这里.

我遇到了一个问题,如果您创建一个任何类型的数组并在 Java 中对该数组调用 getClass,您将得到一个奇怪的回报.我想知道为什么你会得到这个特定的回报以及它意味着什么.

代码示例如下:

byte[] me = new byte[1];int[] me2 = new int[1];双[] me3 = 新双[1];浮动[] me4 = 新浮动[1];String[] me5 = new String[1];整数[] me6 = 新整数[1];System.out.println(me.getClass());System.out.println(me2.getClass());System.out.println(me3.getClass());System.out.println(me4.getClass());System.out.println(me5.getClass());System.out.println(me6.getClass());

输出为:

 类 [B类 [I[D级][F级]类 [Ljava.lang.String;类 [Ljava.lang.Integer;

解决方案

toString 类的方法调用 getName 类的方法 which

<块引用>

返回实体的名称(类、接口、数组类、原始类型,或 void)由这个 Class 对象表示,作为细绳.如果这个类对象代表的引用类型不是一个数组类型,然后返回类的二进制名称,如由 Java™ 语言规范指定.

如果这个类对象表示一个原始类型或者void,那么返回的名称是一个等于 Java 语言关键字的字符串对应于原始类型或void.

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

<前>元素类型编码布尔值 Z字节B字符 C类或接口 Lclassname;双D浮点数国际我长J短裤

类或接口名称 classname 是类的二进制名称如上所述.

示例:

 String.class.getName()返回java.lang.String"byte.class.getName()返回字节"(new Object[3]).getClass().getName()返回[Ljava.lang.Object;"(new int[3][4][5][6][7][8][9]).getClass().getName()返回[[[[[[[[我"

So I've run into something odd and I don't know what it's called so I'm having trouble finding out information about it, hence my question here.

I've run into an issue where if you create an array of any type and call getClass on this array in Java you will get an odd return. I am wondering why you get this specific return and what it means.

Code example is as follows:

byte[] me = new byte[1];
int[] me2 = new int[1];
double[] me3 = new double[1];
float[] me4 = new float[1];
String[] me5 = new String[1];
Integer[] me6 = new Integer[1];

System.out.println(me.getClass());                  
System.out.println(me2.getClass());                 
System.out.println(me3.getClass());                 
System.out.println(me4.getClass());                 
System.out.println(me5.getClass());
System.out.println(me6.getClass());

and the output is:

 class [B
 class [I
 class [D
 class [F
 class [Ljava.lang.String;
 class [Ljava.lang.Integer;

解决方案

The toString method of Class invokes the getName method of Class which

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. 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.

If this class object represents a primitive type or void, then the name returned is a String equal to 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
 class or interface     Lclassname;
 double                 D
 float                  F
 int                    I
 long                   J
 short                  S 

The class or interface name classname is the binary name of the class specified above.

Examples:

 String.class.getName()
     returns "java.lang.String"
 byte.class.getName()
     returns "byte"
 (new Object[3]).getClass().getName()
     returns "[Ljava.lang.Object;"
 (new int[3][4][5][6][7][8][9]).getClass().getName()
     returns "[[[[[[[I"

这篇关于具有数组类型的 getClass 方法 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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