首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java面试|精选基础题(2)

java面试|精选基础题(2)

作者头像
KEN DO EVERTHING
发布2019-01-17 14:47:50
9171
发布2019-01-17 14:47:50
举报
文章被收录于专栏:KEN DO EVERTHINGKEN DO EVERTHING

阅读本文大概需要6分钟

继续挖掘一些有趣的基础面试题,有错望指出来哈,请赐教~

1.包装类的装箱与拆箱

简单一点说,装箱就是自动将基本数据类型转换为包装器类型;拆箱就是自动将包装器类型转为基本数据类型。 那它又是如何实现的? 以Integer为例,看下代码:

public class Box {
    public static void main(String [] args){
        Integer i = 10;//装箱
        int n = i;//拆箱
    }
}

反编译class文件之后得到如下内容:

D:\Study\java\jdk8\bin\javap.exe -c upBox.Box
Compiled from "Box.java"
public class upBox.Box {
  public upBox.Box();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        10
       2: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       5: astore_1
       6: aload_1
       7: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      10: istore_2
      11: return
}

从反编译得到的字节码内容可以看出,在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法,其他的包装类类似。

2.Integer类型的比较

深藏陷阱的面试题:

public class Test {
    public static void main(String[] args) {
        Integer f1 = 100;
        Integer f2 = 100;
        Integer f3 = 150;
        Integer f4 = 150;

        System.out.println(f1 == f2);  //true
        System.out.println(f3 == f4);  //false
    }
}

使用==对比的是引用是否相等,这很容易让人误认为两个输出都是true或false。

来分析一波:从上一题中可知道Integer装箱调用静态方法valueOf,我们来看下valueOf的源码

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

可以看出,若int的值IntegerCache的最小值至最大值之间,则返回IntegerCache中的值,否则返回Integer对象;

IntegerCache是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=<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() {}
    }

IntegerCache 是Integer中的静态类,虚拟机加载Integer类就会将[-128,127]的值存储在Integer cache[]中。

由以上两段源码可得出:将int赋值给Integer时,若int的值在[-128,127]内,则会直接引用Intefer缓存池中的对象;不在,则创建新的Integer对象。 返回去看面试题,是否已一目了然?

3.序列化和反序列化
  • 概念 序列化:把Java对象转换为字节序列的过程。 反序列化:把字节序列恢复为Java对象的过程。
  • 序列化的实现 将需要被序列化的类实现Serializable接口
  • 用途 1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中; 2) 在网络上传送对象的字节序列。
  • 例子
import java.io.*;
import java.util.Date;

public class ObjectSaver {
    public static void main(String[] args) throws Exception {
        /*其中的  D:\\objectFile.obj 表示存放序列化对象的文件*/


        //序列化对象
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj"));
        Customer customer = new Customer("张三", 25);    
        out.writeObject("hello!");    //写入字面值常量
        out.writeObject(new Date());    //写入匿名Date对象
        out.writeObject(customer);    //写入customer对象
        out.close();


        //反序列化对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj"));
        System.out.println("String:" + (String) in.readObject());    //读取字面值常量
        System.out.println("Date:" + (Date) in.readObject());    //读取匿名Date对象
        Customer obj3 = (Customer) in.readObject();    //读取customer对象
        System.out.println("Customer:" + obj3.toString());
        in.close();
    }
}

class Customer implements Serializable {
    private String name;
    private int age;
    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "name=" + name + ", age=" + age;
    }
}

执行结果: String:hello! Date:Sun Jul 08 11:38:17 GMT+08:00 2018 Customer:name=张三 age=25

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-07-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java从心 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.包装类的装箱与拆箱
  • 2.Integer类型的比较
  • 3.序列化和反序列化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档