Java基础精选,你答对了几道?

没有技术深度是大多程序员的一种常态。

但是当你成为一个资深的工程师的时候,很多公司并不希望你还是那样平庸,没有深度。虽然你会纳闷,我就算有深度你们也不一定用得上呀?然而到了这个级别的人需求量并不像初中级开发那么多,公司更理性和稳妥的做法是选择有深度的人,不是吗?

 

Integer比较

看下面这段有意思的代码,对数字比较敏感的小伙伴有没有发现异常?

public static void main(String[] args) {
       Integer a = 128,b=128;  
       Integer c = 127,d=127;
             
       System.out.println(a==b);  
       System.out.println(c==d);
}

如果你的回答是false,false,可能你有一定的基础,知道Integer是一个封装类。当然如果你的答案是true,true的话,也在一定的认知范围之内,但是基础知识掌握的不够好。

  

好了,我们运行main方法,正确答案应该是false,true。前几年这道题出现在很多面试题中,当然你也会说了,我会做项目就ok了,用到查就是了,何必要知道,这我没话说。

其实当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,让我们看下源代码是怎么实现的。

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 -XX:AutoBoxCacheMax=<size> 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.
    */

   private static class IntegerCache {
       static final int low = -128;
       static final int high;
       static final Integer cache[];

       static {
           // high value may be configured by property
           int h = 127;
           String integerCacheHighPropValue =
               sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
           if (integerCacheHighPropValue != null) {
               int i = parseInt(integerCacheHighPropValue);
               i = Math.max(i, 127);
               // Maximum array size is Integer.MAX_VALUE
               h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
           }
           high = h;

           cache = new Integer[(high - low) + 1];
           int j = low;
           for(int k = 0; k < cache.length; k++)
               cache[k] = new Integer(j++);
       }

       private IntegerCache() {}
   }
public static Integer valueOf(int i) {
       assert IntegerCache.high >= 127;
       if (i >= IntegerCache.low && i <= IntegerCache.high)
           return IntegerCache.cache[i + (-IntegerCache.low)];
       return new Integer(i);
}

神奇不神奇,其实代码描述的很清晰,如果整型字面量的值介于-128到127之间,就不会new一个新的Integer对象,而是直接引用常量池中的Integer对象,所以上面的运行结果是a==b=false,而c==d=true。

 

String比较

接下来这道题,相对来说应该比较简单了。

public static void main(String[] args) {
       String s1 = "abc";  
       String s2 = "abc"; 
       String s3 = new String("abc"); 
       System.out.println(s1 == s2); 
       System.out.println(s1 == s3);
   }

小伙伴们看了是不是很熟悉?可能有的人一眼就扫出了答案true,false。当然没有扫出正确答案的小伙伴们也不要气馁,下面跟大家分析分析为毛是这么一个答案。

按照==的语法来看, 首先s1、s2、s3是三个不同的对象,常理来说,输出都会是false。然而程序的运行结果确实true、false。第二个输出false可以理解,第一个输出true就又让人费解了。

我们知道一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配,而堆内存中则存放new 出来的对象和数组。然而除此之外还有一块区域叫做常量池。

像我们通常想String s1 = "abc";这样申明的字符串对象,其值就是存储在常量池中。当我们创建String s1 = "abc"这样一个对象之后,"abc"就存储到了常量池(也可叫做字符串池)中。

当我们创建引用String s2 = "abc" 的时候,Java底层会优先在常量池中查找是否存在"abc",如果存在则让s2指向这个值,不会重新创建,如果常量池中没有则创建并添加的池中。这就是为什么答案是true 和false的原因。

 

Integer与int比较

public static void main(String[] args) {
       Integer a = new Integer(128);  
       int b = 128; 
       Integer c = new Integer(6); 
       Integer d = new Integer(6); 
       System.out.println(a == b); 
       System.out.println(c == d); 
   }

相信又有不少小伙伴懵比了吧,ture还是false?还是直接公布答案吧,true,false。

c == d=false,我觉得没什么好说的,可能有的小伙伴要问了不是-128-127被缓存起来了么?但是我们这里的Integer是new出来的,并不是用的缓存,所以结果是false。

a == b=true,大家注意一下这里的b是int类型,当int和Integer做==比较的时候,Integer类型会自动拆箱,也就是把Integer转成int类型,所以这里进行比较的是int类型的值,所以结果即为true。

猜你喜欢

转载自blog.csdn.net/qq_35542689/article/details/83142015
今日推荐