感到脸红:int是整形 -128~127 Integer是正整型,你怎么会想到这样的回答,妈的,有脑子吗?!!!

1,int是基本数据类型,初始为0,Integer为封装类,初始为null

①无论如何,Integer与new Integer不会相等。不会经历拆箱过程,i3的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false
  ②两个都是非new出来的Integer,如果数在-128到127之间,则是true,否则为false
  java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存
  ③两个都是new出来的,都为false
  ④int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比

/**
* Returns a {@code Integer} instance for the specified integer value.
* <p>
* If it is not necessary to get a new {@code Integer} instance, it is
* recommended to use this method instead of the constructor, since it
* maintains a cache of instances which may result in better performance.
*
* @param i
* the integer value to store in the instance.
* @return a {@code Integer} instance containing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
return i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128];
}
package com.test;
/**
*
* @author 刘玲
*
*/
public class TestInteger { /**
* @param args
*/
public static void main(String[] args) {
int i = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
//Integer会自动拆箱为int,所以为true
System.out.println(i == i2);
System.out.println(i == i3);
System.out.println("**************");
Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);
Integer i6 = 127;
System.out.println(i5 == i6);//true
/*Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false
*/ Integer ii5 = new Integer(127);
System.out.println(i5 == ii5); //false
Integer i7 = new Integer(128);
Integer i8 = new Integer(123);
System.out.println(i7 == i8); //false
} }
来源:http://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html
另外:
boolean,int,long,double称基本类型,创建后置于stac(堆栈)k中,存取速度很快,而其他引用对象创建后置于heap(堆)中,速度没有堆栈快。
创建方法不同: int i = 1;//此创建了一个基本类型的int对象。
Integer integer = new Integer();//此创建了一个Integer包装类的引用,在内存中的位置不同。
类似的还有char和Character、float和Float、short和Short、byte和Byte
基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型

java 基础 --int 和Integer的区别的更多相关文章

  1. java 中int与integer的区别

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

  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的区别

    首先说一下int和Integer的区别: int 是基本数据类型,Integer是int的包装类.注意:后者的类型是"类".例如使用泛型,List<Integer> n ...

  4. java中int和Integer的区别

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

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

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

  6. Java中int与Integer的区别

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

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

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

  8. java里int和Integer什么区别

    Integer i=0; i是一个对象 int i=3; i是一个基础变量 Integer i=0; 这种写法如果没记错,在JAVA1.5之前是会报错的,自动的加解包是1.5的新特性 必须写成 Int ...

  9. java int与integer的区别

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

随机推荐

  1. [POJ3090]Visible Lattice Points(欧拉函数)

    答案为3+2*∑φ(i),(i=2 to n) Code #include <cstdio> int T,n,A[1010]; void Init(){ for(int i=2;i< ...

  2. Python 入门(一)

    IDE 个人推荐  Pycharm : 比较好用,虽然没有中文,但是练练英语也不错,毕竟大同小异 基础语法 行与缩进 python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} . 缩进的 ...

  3. windows下安装mongodb的崩溃史

    一.下载 官方网站的下载页面打不开https://www.mongodb.com/download-center?jmp=nav 问朋友要了一份,是3.6的,下载安装会卡死.弄了一个小时也半点反应没有 ...

  4. TensorFlow之tf.nn.dropout():防止模型训练过程中的过拟合问题

    一:适用范围: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层 二:原理: dropout就是在不同的训练过程中随机扔掉一部分神经元.也就是让 ...

  5. 北京Uber优步司机奖励政策(3月12日~3月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 成都Uber优步司机奖励政策(2月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. 单目、双目和RGB-D视觉SLAM初始化比较

    无论单目.双目还是RGB-D,首先是将从摄像头或者数据集中读入的图像封装成Frame类型对象: 首先都需要将彩色图像处理成灰度图像,继而将图片封装成帧. (1) 单目 mCurrentFrame = ...

  8. 13、Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  9. 问题:Visual Studio 2017 无法推送到github:The requested URL returned error: 403

    问题: Visual Studio 2017 无法推送到github:The requested URL returned error: 403 原因分析: Visual Studio 2017记录的 ...

  10. OSG的组成结构

    OSG的组成结构 核心结构 OSG的功能类采用“命名空间+类名称”的形式来命名.命名空间的命名方式为:第一个单词小写,后继单词的首字母大写,例如osg.osgUtil.osgViewer等:类的名称则 ...