JavaSE8基础 Integer 包装类对象的值不变

礼悟:
     好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
          虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。


javaSE:1.8
             os:windows7 x64
            ide:MyEclipse 2017

代码

package jizuiku.demo;

/**
 * Integer 包装类的对象不能改变其中的值
 *
 * @author 给最苦
 * @version V17.11.06
 */
public class Demo {
	public static void main(String[] args) {
		// 对象引用 i 变没变?
		// 有几个Integer对象?
		Integer i = new Integer(100);

		// 这句话 没有改变 new Integer(100)所创建的对象 的值吗?
		i = i + 3;
		System.out.println(i);
	}
}

代码运行的结果

为了知道 包装类对象的值不变 的原因,给最苦 对上述代码进行反编译

// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name:   Demo.java

package jizuiku.demo;

import java.io.PrintStream;

public class Demo
{

	public Demo()
	{
	}

	public static void main(String args[])
	{
		Integer i = new Integer(100);
		i = Integer.valueOf(i.intValue() + 3);
		System.out.println(i);
	}
}

  (⊙o⊙)…哪里有新对象呀?没看到new呀? 这里就有一个关键的函数 Integer.valueOf(),给最苦 查看这个函数的源代码

    /**
     * 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);
    }

  关键的一句: return new Integer(i); 哦,懂了!



学习资源:《Head First Java》+ Xjad + 源代码 + 清净的心地。如果您有优秀的书籍,也可以推荐给 给最苦。
博文是看书后,融入思考写成的。博文好,是书写得好。博文坏,是 给最苦 没认真。

时间: 2024-08-03 19:44:24

JavaSE8基础 Integer 包装类对象的值不变的相关文章

JavaSE8基础 Integer与int自动转换 自动装箱与拆箱

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo111 { public static void main(String[] args) { //int是基本类型 //Integer是包装类型 //自动装箱是 基本自动变包装 //自动拆箱是 包装自动变基本 Integer i = n

JavaSE8基础 Integer.toXXX int类型变量以二进制 八进制的形式输出

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku2; public class Demo111 { public static void main(String[] args) { show(15); show(-15); } public static void show(int num) { System.out.pr

JavaSE8基础 Integer构造方法 将符合标准的String类型转成int类型

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo000 { public static void main(String[] args) { //字符串符合int规则的 Integer n = new Integer("124567"); System.out.printl

JavaSE8基础 Integer.valueOf 把int类型变为Integer类型

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo1000 { public static void main(String[] args) { Integer i = Integer.valueOf(1234); } } sourceCode: public static Integ

JavaSE8基础 Integer.toString 将十进制类型转为指定进制[2,36]的表达形式

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo010 { public static void main(String[] args) { System.out.println(Integer.toString(127, 2));//127的二进制 System.out.print

JavaSE8基础 Integer.parseInt 将[2,36]进制转为十进制

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo011 { public static void main(String[] args) { System.out.println(Integer.parseInt("100", 2));// 这个字符串是二进制的形式 Sys

JavaSE8基础 Integer.toString 将int类型转为同面值的String类型

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo001 { public static void main(String[] args) { int num = 1;//int 没有 tostring方法 Integer i = new Integer(num);//Integer有

ES6基础之——把对象的值复制到另一个对象里Object.assign()

Object.assign()可以把一个对象的属性复制到另外一个对象里面 先定义一个空白的对象breakfast let breakfast={} 下面就使用Object.assign()方法 第一个参数是接受者,也就是要复制到的那个目标,第二个是复制的源; 例子: let breakfast={} Object.assign( breakfast, { drink:'beer' } ) console.log(breakfast) //{drink:'beer'} 原文地址:https://w

JavaSE8基础 char[] 根据变量的值生成指定长度的一维char数组

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) referenceLinking:   https://zhidao.baidu.com/question/277749568    code: package jizuiku.t02; public class Demo5 { public static void main(String[] args) { int charAr