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

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

问题描述

所以,我碰到一个奇怪的现象,我不知道它叫什么,所以我有麻烦在这里查不到关于它的信息,因此,我的问题。

我碰到的地方,如果你创建任何类型的数组,并调用的getClass这个数组在Java中,你会得到一个奇怪的回报的问题。我很奇怪,为什么你得到这个特定的返回和这意味着什么。

code示例如下:

 字节[]我=新的字节[1];
INT [] ME2 =新INT [1];
双[] = ME3新的双[1];
浮动[] = ME4新的浮动[1];
的String [] = ME5新的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;


解决方案

的<一个href=\"http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Class.html#toString%28%29\">toString类的方法调用的<一个href=\"http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Class.html#getName%28%29\">getName类的方法,该方法


  

返回实体的名称(类,接口,数组类,
  基本类型或无效)再$ P $此Class对象psented,作为
  字符串。


  
  

如果该类对象重新presents引用类型是不是一个数组
  然后键入类的二进制名称返回,由指定的
  Java语言规范,第二版。


  
  

如果该类对象重新presents一个基本类型或void,那么
  返回的名称等于Java语言的关键字的String
  对应的基本类型或void。


  
  

如果该类对象重新presents一类阵列,那么内部
  的名称的形式包括由pceded元素类型$ P ​​$的名称的
  的一个或多个'['字符重新presenting该阵列的深度
  嵌套。元素类型名的编码如下:

 元素类型编码
布尔ž
BYTE B
焦炭ç
类或接口Lclassname;
双D
浮˚F
INT I
龙J
短小号


  
  

类或接口的名称类名是类的二进制名称
  上述规定。


  
  

例如:

  String.class.getName()
     返回java.lang.String中
 byte.class.getName()
     返回字节
 (新对象[3])。的getClass()。的getName()
     返回[Ljava.lang.Object;
 (新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, Second Edition.

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