Java笔试题库(08期)

本文解析了Java笔试题中关于Integer对象比较的题目,解释了==与equals的区别,并详细阐述了自动装箱、拆箱概念及int常量池的使用范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                    Java笔试题库(08期)

见如下代码:

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(输出)为后缀的是字符流。


给大家一张很nice的图片:

(来自百度图库)

推荐阅读

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值