Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入不是对象的基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper class),int的包装类就是Integer,

  从JDK 1.5开始引入了自动装箱/拆箱机制,使得二者可以相互转换。

Java 为每个原始类型提供了包装类型:

原始类型: boolean, char           byte,short,int,long         float,double

包装类型:Boolean,Character           Byte,Short,Integer,Long           Float,Double

 package com.lovo;  

 public class AutoUnboxingTest {  

     public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3; // 将3自动装箱成Integer类型
int c = 3;
System.out.println(a == b); // false 两个引用没有引用同一对象
System.out.println(a == c); // true a自动拆箱成int类型再和c比较
}
}

补充:最近还遇到一个比较经典的面试题,也是自动装箱和拆箱相关的

 public class Test03 {  

     public static void main(String[] args) {
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2); //结果是true
System.out.println(f3 == f4); //结果是false
}
}

  如果不明就里很容易认为两个输出要么都是true要么都是false。首先需要注意的是f1、f2、f3、f4四个变量都是Integer对象,所以下面的==运算比较的不是值而是引用。

  装箱的本质是什么呢?

  当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,如果看看valueOf的源代码就知道发生了什么

 public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

  简单的说,如果字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,

  所以上面的面试题中f1==f2的结果是true,而f3==f4的结果是false。

java 中Int和Integer区别以及相关示例的更多相关文章

  1. java中int和Integer比较

    java中int和Integer比较 一,类型区别 我们知道java中由两种数据类型,即基本类型和对象类型,int就是基本数据类型,而Integer是一个class,也习惯把Integer叫做int的 ...

  2. java中int和Integer的区别?为什么有了int还要有设计Integer?

    参考https://blog.csdn.net/chenliguan/article/details/53888018 https://blog.csdn.net/myme95/article/det ...

  3. java中int和Integer的区别

    Integer与int的种种比较你知道多少?  转载自http://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html 如果面试 ...

  4. java 中int与integer的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  5. 【JAVA】详解在JAVA中int与Integer的区别以及背后的原因。

    区别 首先我们要明确,这两点之间有什么区别? 主要有以下几点: 数据类型不同:int是基础数据类型,而 Integer是包装数据类型: 默认值不同:int的默认值是 0,而 Integer的默认值是 ...

  6. Java中 int和Integer的区别+包装类

    --今天用Integer 和Integer 比较 发现有问题,于是去查了查. 1.Java 中的数据类型分为基本数据类型和引用数据类型 int是基本数据类型,Integer是引用数据类型: Inget ...

  7. Java中int与Integer的区别

    转自https://www.cnblogs.com/guodongdidi/p/6953217.html import java.lang.Integer; public class intDemo{ ...

  8. Java中int与Integer

    一般小写字母开头的是数据类型(如int double),大写字母开头的一般是封装为类(如Double),里面有很多方法,比如实行转换Integer.parseInt(arg0),可以把其他类型的数据转 ...

  9. java中int和Integer对比的一些坑

    --------------------- 作者:狂飙的yellowcong 来源:CSDN 原文:https://blog.csdn.net/yelllowcong/article/details/ ...

随机推荐

  1. spring 3.2 后 annotation-driven 注册新的类

    DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 的使用已经过时! DefaultAnnotationHandlerMa ...

  2. QCon2016 上海会议汇总(2) - 团队管理

    QCon 2016上海日程:http://2016.qconshanghai.com/schedule <当你的团队还支撑不起梦想时> - 链尚网技术合伙人 杨荣伟 Figo讲述了如何训练 ...

  3. 如何让Jackson JSON生成的数据包含的中文以unicode方式编码

    我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...

  4. 每天一个Linux命令(7)pwd命令

    pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根目录)写入标准输出.全部目录使用/分隔.第一个/表示根目录,最后一个目录是当前目录.     (1)用法介绍: pwd[ ...

  5. windows与VMware ubuntu虚拟机实现文件共享

    本文基本摘自:无限挑战者的博客 本人用的系统是win10系统,应该区别不大,实现的是win10系统和VMWare虚拟机Ubantu系统之间的文件共享.到网上找了很多的方法各种各样的方式,由于对Linu ...

  6. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  7. HTML URL 编码:请参阅:http://www.w3school.com.cn/tags/html_ref_urlencode.html

    http://www.w3school.com.cn/tags/html_ref_urlencode.html

  8. 算法(Algorithms)第4版 练习 2.2.9

    package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; publi ...

  9. 算法(Algorithms)第4版 练习 1.5.8

    假设原id数组: 0 1 1 4 4 8 6 1 8 0 输入p = 5, q = 7 则输出结果会出错,最终为: 0 1 1 4 4 1 6 1 8 0 因为当id[p](id[5] = 8)被赋值 ...

  10. Codeforces Round #250 (Div. 2) A, B, C

    A. The Child and Homework time limit per test 1 second memory limit per test 256 megabytes input sta ...