您的位置:首页 > 职场人生

Java面试题二十四

2018-01-05 18:15 211 查看
见如下代码:
public class IntTest {

 public static void main(String[] args) {
   Integer i1 = 100;
   Integer i2 = 100;
   Integer i3 = 150;
   Integer i4 = 150;
   System.out.println(i1 == i2);
   System.out.println(i3 == i4);
 }
}


问题来了:输出的结果是什么?最好先有自己的答案再看分析

,欢迎留言自己的见解!

问题分析:

1,首先要搞清楚==与equal的区别以及各自比较的是什么

        ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相等,只能用==操作符。

        equals方法是用于比较两个独立对象的内容是否相同,就好比去比较两个人的长相是否相同,它比较的两个对象是独立的。

        所以首先弄清楚了,对于上面代码中,我们是比较变量所对应的内存中所存储的数值是否相同。

2,要了解自动装箱和拆箱的概念

        JVM会自动维护八种基本数据类型的常量池。

        在上面代码中,第一次Integer i1 = 100;时会把i1写入常量池,当定义i2时,数据一样,不会再去创建新的对象,所以,第一个输出的是true。

        但是这里Integer并不是八种基本数据类型啊,这里涉及了自动装箱和拆箱的概念。自动装箱就是Java自动将原始类型值转换成对应的对象,比如将int的变量转换成Integer对象,这个过程叫做装箱,反之将Integer对象转换成int类型值,这个过程叫做拆箱。

        因为这里的装箱和拆箱是自动进行的非人为转换,所以就称作为自动装箱和拆箱。原始类型byte,short,char,int,long,float,double和boolean对应的封装类为Byte,Short,Character,Integer,Long,Float,Double,Boolean。

3,int在常量池中的初始化范围

        int常量池中初始化-128~127的范围,所以当为Integer i1,i2=100时,在自动装箱过程中是取自常量池中的数值,而当Integer i3,i4=150时,150不在常量池范围内,所以在自动装箱过程中需new 一个150,所以上面代码第二个输出的是false。

下面贴出int常量池的部分代码供大家参考:

/**
    * 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=<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) {
               try {
                   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);
               } catch( NumberFormatException nfe) {
                   // If the property cannot be parsed into an int, ignore it.
               }
           }
           high = h;

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

           // range [-128, 127] must be interned (JLS7 5.1.7)
           assert IntegerCache.high >= 127;
       }

       private IntegerCache() {}
   }


正确答案:

true    

false

下面哪个流类属于面向字符的输入流(  )

A  BufferedWriter           

B  FileInputStream          

C  ObjectInputStream          

D  InputStreamReader

正确答案:D

        以InputStream(输入)/OutputStream(输出)为后缀的是字节流。

        以Reader(输入)/Writer(输出)为后缀的是字符流。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: