黑马程序员技术交流社区
标题:
反射的问题编译有错误求解
[打印本页]
作者:
赵国刚
时间:
2013-8-14 11:30
标题:
反射的问题编译有错误求解
请看代码注释部分
我想问的是注释带?的地方,请看清问题,是编译报错,不是运行后结果!!!!
//数组与object的关系及其反射类型
int [] a1 = new int[]{4,2};
int [] a2 = new int[4];
int [][]a3 = new int [2][3];
String [] a4 = new String []{"e","a","g","d"};
System.out.println(a1.getClass() == a2.getClass());//(int)类型相同,(一维数组)维度相同则数组的字节码相同
// System.out.println(a1.getClass() == a3.getClass()); 这里编译不通过是因为多维数组没有字节码?
// System.out.println(a1.getClass() == a4.getClass());为什么 编译不通过?
System.out.println(a1.getClass().getSuperclass().getName());
如果是因为类型不同的话,他们不是class类型比较么?
作者:
兜兜转转
时间:
2013-8-14 12:01
反射好像是JDK5.0出现的新特性吧,你看看你的JDK或JRE版本信息。
作者:
gudao20080
时间:
2013-8-14 12:26
本帖最后由 gudao20080 于 2013-8-14 12:33 编辑
你可以查看一下各个数组的字节码是否一致就清楚了
package com.itcast.mapTest;
import java.lang.reflect.*;
public class ReflectTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a1 = new int[]{4,2};
int [] a2 = new int[4];
int [][]a3 = new int [2][3];
String [] a4 = new String []{"e","a","g","d"};
System.out.println(a1.getClass() == a2.getClass());//(int)类型相同,(一维数组)维度相同则数组的字节码相同
//System.out.println(a1.getClass() == a3.getClass()); //这里编译不通过是因为多维数组没有字节码?
//System.out.println(a1.getClass() == a4.getClass());//为什么 编译不通过?
System.out.println("a2: "+a2.getClass()); //获取a2的字节码
System.out.println("a3: "+a3.getClass()); //获取a3 的字节码
System.out.println("a4: "+a4.getClass());
System.out.println(a1.getClass().getSuperclass().getName());
}
}
复制代码
输出结果是:
true
a2: class [I
a3: class [[I
a4: class [Ljava.lang.String;
java.lang.Object
复制代码
可以看出,一维与二维数组他们的字节码文件是不同的,编译不通过是因为它们没有可比性
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2