int和Integer之间的区别和联系
在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型有什么区别和联系哪?下面以int和Integer为例讲讲我的看法。int和Integer非常的像,所有的基本类型和其对应的装箱基本类型都非常的像,但是他们之间也是有区别的,他们之间的区别是什么哪?
为了明白他们之间的区别和联系,我们一起看看下面这段简单的代码和罗织在代码之间的结论吧!
1:TestIntMain.java——包含基本类型和装箱基本类型的区别和联系的结论注释
import java.util.ArrayList;
import java.util.List; public class TestIntMain {
public static void main(String [] args){ /**
* 区别1:
* int 声明的变量不能直接赋值为null
* Integer 声明的变量能直接赋值为null
*/
// int _int_ = null;//不能直接赋null
Integer _Integer_ = null;//可以直接赋null /**
* 区别2:
* int 声明的变量,存储就是一个数值,不能调用方法,它也没有方法可调用
* Integer 声明的变量,是一个对象,它有对应的变量、方法,它可以自由的调用
*/
int _int = 99;
Integer _Integer1 = new Integer(99);
//一个整数值,不能调用方法
// System.out.println("_int type is : "+_int.getClass().getSimpleName()+"_int value is : "+_int.toString());
System.out.println("_int value is : "+_int); //一个对象,可以调用它的方法,属性等等
System.out.println("_Integer1 type is : "+_Integer1.getClass().getName()+" _Integer1 value is : "+_Integer1); /**
* 自动装箱和拆箱
* 自动装箱:我的理解是将基本类型的变量,自动转换为装箱基本类型的对象的过程
* 自动拆箱:我的理解是将装箱基本类型的对象,自动转换为基本类型的变量的过程
* 1:这是在Java1.5发行版本中增加的功能
* 2:目的是为了模糊基本类型和装箱基本类型之间的区别,减少使用装箱基本类型的繁琐性
* 3:这样做也有一定的风险,比如:对于装箱基本类型运用==操作符几乎总是错误的,装箱的操作会导致高的开销和不必要的对象创建
*/
int _int1 = new Integer(99);//对应的匿名对象变成了一个整数值,自动拆箱
int _int2 = _Integer1;//_Integer1对象变成了一个整数值,自动拆箱
// System.out.println("_int1 type is : "+_int1.getClass().getSimpleName()+"_int1 value is : "+_int1.toString());
System.out.println("_int1 value is : "+_int1); Integer _Integer = 99;//对应的整数值,直接给你对象也没问题,自动装箱
System.out.println("_Integer type is : "+_Integer.getClass().getName()+" _Integer value is : "+_Integer); // List<int> list_ = new ArrayList<int>();
List<Integer> list = new ArrayList<Integer>();
list.add(_int);//_int 此时相当于是一个Integer类型的对象了,自动装箱
list.add(_Integer);
}
}
2:TestIntMain.class——包含基本类型和装箱基本类型的自动装箱和拆箱的实现方式
import java.util.ArrayList;
public class TestIntMain {
public TestIntMain() {
}
public static void main(String[] args) {
Object _Integer_ = null;//Integer _Integer_ = null; 编译后是这样的哦
byte _int = 99;//int _int = 99; 编译后是这样的哦
Integer _Integer1 = new Integer(99);
System.out.println("_int value is : " + _int);
System.out.println("_Integer1 type is : " + _Integer1.getClass().getName() + " _Integer1 value is : " + _Integer1);
int _int1 = (new Integer(99)).intValue();//自动拆箱的实现方式
int _int2 = _Integer1.intValue();//自动拆箱的实现方式
System.out.println("_int1 value is : " + _int1);
Integer _Integer = Integer.valueOf(99);//自动装箱的实现方式
System.out.println("_Integer type is : " + _Integer.getClass().getName() + " _Integer value is : " + _Integer);
ArrayList list = new ArrayList();
list.add(Integer.valueOf(_int));//自动装箱的实现方式
list.add(_Integer);
}
}
3:参考
http://mindprod.com/jgloss/intvsinteger.html
http://www.careerride.com/Java-QA-integer-vs-int.aspx
https://www.quora.com/What-is-the-difference-between-an-integer-and-int-in-Java
int和Integer之间的区别和联系的更多相关文章
- java int 与 Integer之间的区别
int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- 在java语言中int 和 Integer 有什么区别
在java语言中int 和 Integer 有什么区别 解答:int是基本数据类型,Integer是int的包装类,属于引用类型
- 一、基础篇--1.1Java基础-int 和 Integer 有什么区别,Integer的值缓存范围
int和Integer的区别 int是基本数据类型,Integer是int的包装类. Integer必须实例化后才能使用,int变量不需要. Integer实际是对象的引用,生成一个新对象实际上是生成 ...
- Java int和Integer包装类的区别和比较
区别: ...
- Java Int和Integer有什么区别?
Int int是我们常说的整型数字,是Java的8个原始数据类型(Primitive Type:boolean.byte.short.char.int.float.double.long)之一.Jav ...
- int 和 Integer 有什么区别
原文地址:https://blog.csdn.net/chenliguan/article/details/53888018 1 int与Integer的基本使用对比 (1)Integer是int的包 ...
- java面试题(杨晓峰)---第七讲谈谈int和integer有什么区别?
理解装箱和拆箱的过程. 对象由三部分组成:对象头,对象实例,对齐填充. 对象头:一般是十六个字节,分两部分,第一部分:哈希码,锁状态标志,线程持有的锁,偏向线程id,gc分代年龄等,第二部分是类型指针 ...
- PHP 中 int 和 integer 类型的区别
半夜整理东西,发现一个以前没留意到的小问题. function show($id) : int { return $id; } function show($id) : integer { retur ...
随机推荐
- (转)No row with the given identifier exists问题的解决
产生此问题的原因: 有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique ...
- [Spring Boot] Introduce to Mockito
We have the implemetion: @SpringBootApplication public class MockitoDemoApplication { public static ...
- XE6入门(一)Hello World
XE6的IDE已经设计的非常棒了,是该放弃D7了,投入XE6的怀抱.. 本人用的XE6版本是 Embarcadero.Delphi.XE6.RTM.Inc.Update1.v20.0.16277.12 ...
- sonarqube 5.6
转载:https://www.jianshu.com/p/402987500bfd 一. 简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量.通过插件形式,可以支持包括java,C ...
- jQuery 超屏加载
jQuery 超屏加载,当文档超出屏幕的高度时,加载最新下个列数据 $(window).scroll(function () { var height = $(document).height(); ...
- asp.net序列化
JsonHelp.cs using System.IO; using System.Text; using System.Runtime.Serialization.Json; namespace W ...
- 树莓派通过GPIO控制步进电机
一.接线方式与GPIO调用方法: 电源接入+5v和GND In1-4分别接GPIO1-4 正转时,GPIO1-4分次传入:[1,0,0,0],[sleep],[0,1,0,0],[sleep],[0, ...
- Java 图片验证码工具
package com.microwisdom.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics ...
- 基于py3和pymysql查询某时间段的数据
#python3 #xiaodeng #基于py3和pymysql查询某时间段的数据 import pymysql conn=pymysql.connect(user='root',passwd='r ...
- 【appium】查看Android应用包名、Activity的几个方法
一.有源码情况 直接打开AndroidManifest.xml文件,找到包含android.intent.action.MAIN和android.intent.category.LAUNCHER对应的 ...