在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型有什么区别和联系哪?下面以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之间的区别和联系的更多相关文章

  1. java int 与 Integer之间的区别

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

  2. Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?

    接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...

  3. 在java语言中int 和 Integer 有什么区别

    在java语言中int 和 Integer 有什么区别 解答:int是基本数据类型,Integer是int的包装类,属于引用类型

  4. 一、基础篇--1.1Java基础-int 和 Integer 有什么区别,Integer的值缓存范围

    int和Integer的区别 int是基本数据类型,Integer是int的包装类. Integer必须实例化后才能使用,int变量不需要. Integer实际是对象的引用,生成一个新对象实际上是生成 ...

  5. Java int和Integer包装类的区别和比较

    区别:                                                                                                  ...

  6. Java Int和Integer有什么区别?

    Int int是我们常说的整型数字,是Java的8个原始数据类型(Primitive Type:boolean.byte.short.char.int.float.double.long)之一.Jav ...

  7. int 和 Integer 有什么区别

    原文地址:https://blog.csdn.net/chenliguan/article/details/53888018 1 int与Integer的基本使用对比 (1)Integer是int的包 ...

  8. java面试题(杨晓峰)---第七讲谈谈int和integer有什么区别?

    理解装箱和拆箱的过程. 对象由三部分组成:对象头,对象实例,对齐填充. 对象头:一般是十六个字节,分两部分,第一部分:哈希码,锁状态标志,线程持有的锁,偏向线程id,gc分代年龄等,第二部分是类型指针 ...

  9. PHP 中 int 和 integer 类型的区别

    半夜整理东西,发现一个以前没留意到的小问题. function show($id) : int { return $id; } function show($id) : integer { retur ...

随机推荐

  1. Swift语言精要-闭包(Closure)

    闭包(Closure)这个概念如果没学过Swift的人应该也不会陌生. 学过Javascript的朋友应该知道,在Javascript中我们经常会讨论闭包,很多前端工程师的面试题也会问到什么是闭包. ...

  2. LintCode: Longest Words

    C++ class Solution { public: /** * @param dictionary: a vector of strings * @return: a vector of str ...

  3. 010-Go 操作PostgreSQL数据库2

    1:sql脚本 create table post( id serial primary key, content text, author ) ) 2:post.go package post im ...

  4. 007-Go package 说明

    1:在项目src下面存在两个目录,每个目录里面各有一个go文件 2:add.go package test02 func Add(a int, b int) int{ return a + b } 注 ...

  5. AT&T汇编和Intel汇编语法主要区别

    AT&T使用$表示立即操作数,而Intel的立即操作数是不需要界定的.因此,使用AT&T语法引用十进制值4时,使用$4,使用Intel语法时只需使用4.   AT&T在寄存器名 ...

  6. 查看JAVA的class二进制文件的方法

    hexdump -C filename可以查看二进制文件. 比如java的Test.java public class Test{ public static void main(String[] a ...

  7. python的traceback模块

    import traceback try: 1/0 except Exception,e: traceback.print_exc() 输出结果是 Traceback (most recent cal ...

  8. mysql标准写法及其他常见问题

    /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50549 Source H ...

  9. java动态代理_aop

    (一)代理概述 1.问题:要为已存在的多个具有相同接口的目标类的各个方法增加一些系统功能,例如,异常处理.日志.计算方法的运行时间.事务管理等等,如何去做? 解答:编写一个与目标类具有相同接口的代理类 ...

  10. java for语句

    //for语句 public class Test16{ public static void main(String args[]){ for (int i=0;i<10;i+=1){ if ...