java Integer和int的拆箱与装箱
官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
1、赋值:
a. 把int类型赋值给Integer类型:JVM会自动调用Integer.valueOf()方法
b. 把Integer类型赋值给int类型:JVM会自动调用intValue()方法
2、比较
a. int 与 int 比较:直接对两个变量的值进行比较
b. int 与 Integer 比较:会先把Integer拆装成int类型,然后进行比较
c. Integer 与 Integer比较:比较两个对象的引用地址
值得注意的是:对Integer对象,JVM会自动缓存-128~127范围内的值,所以所有在这个范围内的值相等的Integer对象都会共用一块内存,而不会开辟多个;超出这个范围内的值对应的Integer对象有多少个就开辟多少个内存。底层代码如下:
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
3、测试代码:
@Test
public void test1() {
Integer num1 = 100;
Integer num2 = 100;
System.out.println("test1 result is: " + (num1 == num2)); // true
}
@Test
public void test2() {
Integer a = 128;
Integer b = 128;
System.out.println("test2 result is: " + (a == b)); // false
}
@Test
public void test3() {
Integer a = 100;
Integer b = new Integer(100);
System.out.println("test3 result is: " + (a == b)); // false
}
java Integer和int的拆箱与装箱的更多相关文章
- java 基本数据类型的自动拆箱与装箱
——> -128~127之间的特殊性.为什么要这样设计,好处? ——> 享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实 ...
- int和Integer及拆箱与装箱
int和Integer 如果面试官问Integer与int的区别:估计大多数人只会说道两点,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null.但是如果面试官再问一下In ...
- JAVA进阶之旅(一)——增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法
JAVA进阶之旅(一)--增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法 学完我们的java之旅,其实收获还是很多的,但是依然还有很 ...
- Java Integer 与 int 深刻理解
今天在做Object 自动转为Integer 类型之后的判断,遇到一个不理解的点,当数值超过127之后,两个数值相同的Object 对象用 == 判断的结果是false. Object a = 128 ...
- Java知多少(24)包装类、拆箱和装箱详解
虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...
- JAVA中拆箱和装箱
浅谈JAVA中拆箱与装箱 一. 什么是装箱?什么是拆箱? 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10) ...
- [Java学习] Java包装类、拆箱和装箱详解
虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...
- Java包装类、拆箱和装箱详解
转载:https://www.cnblogs.com/ok932343846/p/6749488.html 虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程, ...
- 《Java基础知识》Java包装类,拆箱和装箱
虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...
随机推荐
- Oauth 2.0第三方账号登录原理图
百度.QQ等服务商
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- SQL触发器中若取到null值可能引发的问题
declare @code varchar(20), @cs varchar(20),@zc varchar(20)set @cs='('+@cs+'*'+@zc+')'print '字符'+@csi ...
- mysql格式化整数类型时间生成年月日时分秒格式(long或string接收)
数据库格式: 数据库mysql语句: FROM_UNIXTIME( s.timemodified, '%Y-%m-%d %h:%i:%s' ) 生成结果: 测试sql为: SELECT *, FROM ...
- simple demo how to get the list of online users
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...
- 经典排序算法 - 冒泡排序Bubble sort
原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子 ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- C#导出Excel动态列
一.用StreamWrite流对象,导出Excel 1. string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagn ...
- php curl get post
post有3种. 1.post方式 privatefunction send_post($url,$post_data){ $ch = curl_init($url); curl_setopt($ch ...
- SQL Server output经典使用
output经典使用 分类: sql2012-02-16 18:17 409人阅读 评论(0) 收藏 举报 outputinserttabledeletegonull OUTPUT是SQL SERVE ...