感到脸红: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. Java 8 中有趣的操作 Stream

    Stream 不是java io中的stream 对象创建 我们没有必要使用一个迭代来创建对象,直接使用流就可以 String[] strs = {"haha","hoh ...

  2. window下创建虚拟环境

    一. windows下创建虚拟环境 1. 终端下执行命令:python -m pip install -upgrade pip 2. pip install virtualenv 3. 在本地创建一个 ...

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

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

  4. 抽样分布(3) F分布

    定义 设U~χ2(n1), V~χ2(n2),且U,V相互独立,则称随机变量 服从自由度为(n1,n2)的F分布,记为F~F(n1,n2),其中n1叫做第一自由度,n2叫做第二自由度. F分布的概率密 ...

  5. leetcode笔记9 Move Zeroes

    题目要求: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  6. C#调用大漠插件,发送QQ和微信消息

    大漠插件就不过多介绍了,不知道的请查下百度.主要是讲解C#怎么调用大漠插件. 大漠插件提供了COM版本,C#直接点击引用,添加即可.然后注册下大漠插件到系统文件夹,注册代码如下: static str ...

  7. “腾讯WeTest助力《龙珠直播》盘点APP质量问题”

    WeTest 导读 据调查数据表明,移动端用户在使用APP时如果遇到了闪退等兼容性问题,20%的用户会选择直接卸载. 2016年,被称为中国直播元年.随着各类直播平台的疯狂生长与扩散,直播产品在内容, ...

  8. Visual Studio 智能提示功能消失解决办法

    步骤如下: 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visual Studio Tools --> Developer Command ...

  9. C# Builder

    如下: class Program { static void Main(string[] args) { ).BuildB(2.1).BuildUp(); Console.Read(); } } p ...

  10. git 操作几个命令

     git clone ssh://lijianfeng@192.168.1.246:29418/GMGameSDK压栈:git stash查状态:git status切换到要修改的提交:git reb ...