每日一句

昨日翻译

Suffering is the most powerful teacher of life.

苦难是人生最强大的老师

今日名言

Great hopes make great men. 


2019.03.09问题及解析


public class IntegerTest {    public static void main(String[] args) {        Integer num1 = 1;        Integer num2 = 1;        Integer num3 = 222;        Integer num4 = 222;        System.out.println(num1==num2);        System.out.println(num3==num4);    }}


请问上述程序的输出结果是什么?

答案与解析

跑过代码的小伙伴们都知道,输出的结果是:

true

false

这是为什么呢?这明明看着都是一模一样的,为什么却会输出两个截然不同的结果呢?

我们就得了解一下java.lang.Integer类,我们都知道基本数据类型中有一个int,这个Integer看着和它好像,他们之间会不会有什么联系呢?

我们一起来看一下Integer的API中说了什么

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

Integer类以一个对象的方式包裹了一个基本类型int的值。一个object类型的Integer包含了包含了一个类型是int类型的单独的成员变量。

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

并且这个类提供了多种方法,比如转换一个int类型为一个String类型,或者把一个String类型转换为int类型,还包含了其他当你要处理int时有用的常量和方法

原来这个类包含了处理int的各种方法和常量,开始提到了一个单词wraps,就是包裹的意思,那么我们java中也就习惯把这种和Integer相似的类统一称作包装类。

那么Integer是一个类,所以我们用“==”进行比较时,必然是比较的是地址,而不是值,那就说明赋值1给它时,他们指向的是同一个对象,这到底是发生了什么呢?

我们来看一下Integer中有一个私有的静态内部类IntegerCache,看一下看它的注释

* Cache to support the object identity semantics of autoboxing for values between     * -128 and 127 (inclusive) as required by JLS.     *     * The cache is initialized on first usage.  The size of the cache     * may be controlled by the {@code -XX:AutoBoxCacheMax=} option.     * During VM initialization, java.lang.Integer.IntegerCache.high property     * may be set and saved in the private system properties in the     * sun.misc.VM class.* Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.

那么这里提到了当我们给一个-128到127的值进行自动包装时(也就是我们经常听到的装箱),它用来支持他们成为同一个身份。

根据他的类名,Integer缓存,也就是通过缓存的方式当我们给-128到127的值包装时会指向同一个对象,所以开始我们的num1==num2返回了true,同时我们的值可以不用是1,而是-128到127的任何一个数,他们都会返回true。

后面的false自然是num3、num4都重新创建了新的对象,他们指向的地址自然不同,因此返回false

所以最终会返回

true

false

今天又不得不表扬Carlos?和lmbby小伙伴了,希望大家都去看看他们的留言,能学到很多拆装箱的知识哦!

坚持总会有回报的,大家加油!


2019.03.10问题


public class StringTest {//    static String s;//语句1    public static void main(String[] args) {//        String s;//语句2        System.out.println("s="+s);    }}

1.请问取消语句1的注释会输出什么?

2.请问取消语句2的注释会输出什么?

3.请问取消语句1和语句2的注释会输出什么?


往期回顾(3.5~3.9)


java每日一练(19_03_05) (System.out.println();的调用)

java每日一练(19_03_06) (基本数据类型的相互转换)

java每日一练(19_03_07) (MVC框架)

java每日一练(19_03_08) (程序题 异常 输出)

java每日一练(19_03_09) (程序的输出结果)


少年少女们,和我签订契约~变得更强吧!


     眼过千遍,不如手过一遍,一定一定要把自己的想法写出来(想法不论对错,写你所想就是进步),这样才会有所收获,你的每一个留言和分享小刀都会认真回复,彼此学习共同进步~


java写ai JAVA写爱心_微信

END

java写ai JAVA写爱心_微信