java面試| 精選基礎題(2)

關注微信公衆號「java從心」,置頂公衆號
天天進步一點點,距離大腿又近一步!
閱讀本文大概須要6分鐘
繼續挖掘一些有趣的基礎面試題,有錯望指出來哈,請賜教~

1.包裝類的裝箱與拆箱

簡單一點說,裝箱就是自動將基本數據類型轉換爲包裝器類型;拆箱就是自動將包裝器類型轉爲基本數據類型
那它又是如何實現的?
以Integer爲例,看下代碼:java

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.BoxCompiled 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方法,其餘的包裝類相似。shell

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[]中。ide

由以上兩段源碼可得出:將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

推薦閱讀:

限時領取| 熱門編程語言學習資源,不收藏嗎?(文末送書)

實用代碼| shell腳本實現定時檢查應用狀態

java面試| 精選基礎題(1)

end~thanks!

image

一個立志成大腿而天天努力奮鬥的年輕人

期待你的到來!

微信公衆號,"java從心"

相關文章
相關標籤/搜索