Java Integer.class VS Integer.TYPE VS int.class

更多 Java 基础知识方面的文章,请参见文集《Java 基础知识》


Integer.class VS int.class

相同点:都会得到 Class<Integer>

不同点:

  • Integer 是 Object Type 对象类型,int 是 Primitive Type 原始类型
  • Integer 有成员变量,有成员方法,int 无成员变量,无成员方法
  • Integer:a reference to an int primitive
  • int:a literal numerical value

示例:

public static void main(String[] args) {
    Integer i1 = 123;
    int i2 = 123;

    Class<Integer> c1 = Integer.class;
    Class<Integer> c2 = int.class;

    // False
    System.out.println(c1 == c2);
    // False
    System.out.println(c1.isPrimitive());
    // True
    System.out.println(c2.isPrimitive());
}

Integer.TYPE

得到 Class<Integer>:The class instance representing the primitive type int。
因此:

  • Integer.classint.class 不同
  • Integer.TYPEint.class 相同

示例:

public static void main(String[] args) {
    Integer i1 = 123;
    int i2 = 123;

    Class<Integer> c1 = Integer.TYPE;
    Class<Integer> c2 = int.class;

    // True
    System.out.println(c1 == c2);
    // True
    System.out.println(c1.isPrimitive());
    // True
    System.out.println(c2.isPrimitive());
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 2,082评论 0 3
  • "Unterminated string literal.": "未终止的字符串文本。", "Identifier...
    两个心阅读 8,493评论 0 4
  • 太阳赢 双方目前都处于担心与忧虑当中,对赛事,对手充满了不确定~ 太阳队相对更加稳定与扎实写好,稳扎稳打,进球次数...
    意守两眉阅读 336评论 0 0
  • 有这样一句“慢性病不可怕,可怕的是慢性病综合症”。我家人的糖尿病,其实不是很高,但是由于错过了最佳的治疗期,出...
    健康顾问飞雁阅读 678评论 0 0