A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谢洋 高级黑马   /  2013-2-24 11:03  /  2816 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

System.out.println(int.class == Integer.class);//false
System.out.println(int.class == Integer.TYPE);//true
System.out.println(Integer.class == Integer.TYPE);//false

被搞晕了,同一个TPYE是同一分字节码?

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

4 个回复

倒序浏览
一、Integer.TYPE :(int.class)
                返回的是 int (Class<?>)
        
        而
   
二、Integer.class:
                返回是的 Integer 类的对象。

1、我怎么都越想越是觉得都是一样的哈···
2、我下面新建一个 3维 数组:
int[] dim = new int[] { 5, 10, 15 };
// array    是 3维数组.
Object array = Array.newInstance(Integer.TYPE, dim);
//componentType - the Class object representing the component type of the new array
按照JDK 说的
我总感觉应该用 Integer.class
因为我的输出:
    System.out.println(Integer.TYPE); -> outPut:int
    System.out.println(Integer.class); -> outPut:class java.lang.Intege
回复 使用道具 举报
int.class==Integer.TYPE,而Integer.class返回的是Integer类所对应的Class对象。(其他7个原生数据类型同Integer)。
再对应你这道题,一看就明白了。
另外8个原生数据类型为:byte,short,int,long,char,boolean,float,double
它们都有属于自己的对象引用类型:Byte,Short,Integer,Long,Character,Boolean,Float,Double

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报

                Class clazz1 = Integer.TYPE;
                Class clazz2 = Integer.class;
                Class clazz3 = int.class;
                System.out.println(clazz1.getName());//表示基本类型 int 的 Class 实例
                System.out.println(clazz2.getName());//表示类Integer的class实例
                System.out.println(clazz3.getName());//表示基本类型 int 的 Class 实例
                System.out.println(clazz1==clazz3);//此处为True
                System.out.println(clazz1==clazz2);//此处为false

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
本帖最后由 esirong 于 2013-2-24 12:07 编辑

Class c = int.class; 或者 Class c = Integer.TYPE;
它们可获得基本类型的类信息。其中后一种方法中访问的是基本类型的封装类 (如 Integer) 中预先定义好的 TYPE 字段。
Integer.TYPE 表示基本类型 int 的 Class 实例。
public static void main(String[] args){
        Integer a = 200;
        Integer b = 400;
        System.out.println(a.TYPE == b.TYPE);
        }

结果是true;
同一个TPYE是同一分字节码

点评

都24分怎么还没改名哦  发表于 2013-2-25 20:51
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马