JavaSE8 the value of the base Integer wrapper class object does not change

Source: Internet
Author: User
Tags wrapper

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See Root 321, rivers with one.
Meekness Conscience Lord, willing to do without regrets to the most bitter. Reading exercise strong body and mind, Prudential advised and the line and cherish.



javase:1.8
Os:windows7 x64
Ide:myeclipse



Code

Package jizuiku.demo;/** * Integer wrapper class object cannot change the value in the *  * @author to the hardest * @version V17.11.06 */public class Demo {public sta tic void Main (string[] args) {//object reference I has not changed?//How many integer objects? Integer i = new integer (100);//Does this sentence change the value of the object created by the new Integer (100)? i = i + 3; System.out.println (i);}}

The result of the code run

In order to know the reason that the value of the wrapper class object is not changed, the above code is recompiled to the most bitter

Decompiled by Jad V1.5.8e2. Copyright 2001 Pavel kouznetsov.//Jad Home page:http://kpdus.tripod.com/jad.html//decompiler options:packimports (3) F Ieldsfirst ANSI Space//Source File Name:   demo.javapackage Jizuiku.demo;import Java.io.printstream;public class  Demo{public Demo () {}public static void Main (String args[]) {Integer i = new Integer (+); i = integer.valueof (I.intvalue () + 3); System.out.println (i);}}

(⊙o⊙) ... Where is the new object? Didn't you see new? Here is a key function integer.valueof (), giving the most bitter view of the source code of this function

    /**     * Returns an {@code Integer} instance representing the specified     * {@code int} value.  If a new {@code Integer} instance is not     * required, this method should generally being used in preference to     * the C Onstructor {@link #Integer (int)}, as this method was likely     * to yield significantly better space and time performance by     * Caching frequently requested values.     * This method would always have     the cache values in the range-128 to 127,     * inclusive, and may caches other values Outsid E 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);    }

Key sentence: return new Integer (i); Oh, I understand!

Learning resources: "Head first Java" + Xjad + source code + Pure heart. If you have excellent books, you can also recommend to give the most bitter.
Blog post is a book, into the thinking written. It's good to write a blog post. Blog bad, is to give the most bitter not serious.

JavaSE8 the value of the base Integer wrapper class object does not change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.