感到脸红: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. Python学习1——关于变量

    在python中,使用变量之前不需要声明变量的数据类型, 但是,使用变量前,必须要先对变量进行赋值: 例: num01 += 100 print('num01') 上述例子中,表示的意思是 num01 ...

  2. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

  3. notpad++ 搭配 gcc

    notpad++ 搭配 gcc GCC 是 GNU 编译器套装的简称(GNU Compiler Collection),一套编程语言编译器,以 GPL 及 LGPL 许可证所发行的自由软件,也是 GN ...

  4. go内建容器-字符和字符串操作

    1.基础定义 在基础语法篇提到过golang的rune相当于其他编程语言的char,其本质是一个int32(四字节),用[]rune来转换一个字符串时,得到的是个解码后的结果,存储在新开辟的[]run ...

  5. 安装Flutter环境

    mac 环境安装 系统需求 操作系统: macOS (64-bit) 硬盘: 700 MB 工具: bash, mkdir, rm, git, curl, unzip, which 环境安装 SDK ...

  6. Spring Cloud 分布式事务管理

    Spring Cloud 分布式事务管理 在微服务如火如荼的情况下,越来越多的项目开始尝试改造成微服务架构,微服务即带来了项目开发的方便性,又提高了运维难度以及网络不可靠的概率. Spring Clo ...

  7. 滑雪_KEY

    滑雪 ( skiing.pas/c/cpp) [题目描述] MM 参加一个滑雪比赛,滑雪场是一个 N×M 的矩形, MM 要从起点( 1, 1)滑到( N,M).矩形中每个单位格子有一个海拔高度值 h ...

  8. PostgreSQL 使用总结

    1. USING的使用 USING是个缩写的概念:它接收一个用逗号分隔的字段名字列表, 这些字段必须是连接表共有的,最终形成一个连接条件,表示这些字段对必须相同. USING (a, b, c) 等效 ...

  9. facebook原生广告添加adchoice图标

    1.在需要显示adchoice的地方添加一个textview: <LinearLayout android:id="@+id/ad_ic_action" android:la ...

  10. 一对多,多的逗号分隔存在新字段中(Group_concat 用法)

    sql 语句: SELECT    (        SELECT            Group_concat(t_work_group_user.user_id)        FROM     ...