Java基礎知識常見面試題彙總 第一篇

1. 前言

參加過社招的同窗都瞭解,進入一家公司面試開發崗位時,填寫完我的信息後,通常都會讓先作一份筆試題,而後公司會根據筆試題的回答結果,肯定要不要繼續這次面試,若是答的很差,有些公司可能會直接說「技術經理或者總監在忙,你先回去等通知吧」,有些公司可能會繼續面試,瞭解下你的項目經驗等狀況。html

至少在工做的前5年甚至更久,面試通常不會跳過筆試題這個環節(大牛,個別公司除外),我本身也記不清本身面試過多少家公司,作過多少份面試題了,致使如今有時逛街,總感受不少地方似曾相識,感受本身多年前曾經來面過試,一度自嘲,一度也懷疑,本身當年是靠什麼在上海堅持下來的,因此說面試題對於求職來講,仍是很是重要的。java

網上搜索「Java面試題」幾個關鍵字也是有不少不少的文章講解,爲何我還要本身總結呢?主要有如下幾個緣由:面試

  • 文章太多,反倒不知道該看哪一個(就如一本書中所說太多的資訊等於沒有資訊)
  • 文章的準確性不高(曾屢次發現描述不正確或代碼跑不起來的狀況)
  • 能夠加深本身的理解和記憶
  • 一勞永逸,下次不用再從網上慢慢篩選,看本身整理的就行了

本篇主要整理下Java基礎知識的面試題,主要包含如下幾點:spring

  1. Integer和int的區別
  2. ==和equals的區別
  3. String,StringBuilder,StringBuffer的區別
  4. 裝箱和拆箱
  5. Java中的值傳遞和引用傳遞

接下來一一講解。數組

2. Integer和int的區別

2.1 基本概念區分

  1. Integer是int的包裝類(引用類型),int是Java的一種基本數據類型(值類型)。
  2. Integer變量必須實例化後才能使用,而int變量不須要。
  3. Integer實際是對象的引用,當new一個Integer時,其實是生成一個指針指向此對象,而int則是直接存儲數據值。
  4. Integer的默認值是null,int的默認值是0。

2.2 Integer與int常見的幾種比較場景

1)兩個new Integer()變量相比較,永遠返回false緩存

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.println(i == j); // false
複製代碼

兩個經過new生成的Integer變量生成的是兩個對象,其內存地址不一樣安全

2)非new生成的Integer變量和new Integer()生成的變量相比較,永遠返回falsespringboot

Integer i = new Integer(100);
Integer j = 100;
System.out.println(i == j); // false
複製代碼

非new生成的Integer變量指向的是Java常量池中的對象,而new Integer()生成的變量指向堆中新建的對象,二者在內存中的地址不一樣微信

3)兩個非new生成的Integer變量比較,若是兩個變量的值在區間-128到127 之間,則比較結果爲true,若是兩個變量的值不在此區間,則比較結果爲 false。多線程

Integer i = 100;
Integer j = 100;
System.out.println(i == j); //true

Integer i1 = 128;
Integer j1 = 128;
System.out.println(i1 == j1); //false
複製代碼

爲何會這樣呢,咱們來分析下緣由:

Integer i = 100; 在編譯時,會翻譯成 Integer i = Integer.valueOf(100); ,而Java中Integer類的valueOf方法的源碼以下:

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

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() {}
}
複製代碼

從源碼能夠看出:

Java中的Integer對於-128到127之間的數,會進行緩存。 因此 Integer i = 100 時,會將100進行緩存,下次再寫Integer j = 100時,就會直接從緩存中取,而不會new了。

4)Integer變量和int變量比較時,只要兩個變量的值是向等的,則結果爲true

Integer i = new Integer(100);
int j = 100;
System.out.print(i == j); //true
複製代碼

由於包裝類Integer和基本數據類型int比較時,Java會自動拆箱爲int,而後進行比較,實際上就變爲兩個int變量的比較

3. ==和equals的區別

3.1 基本概念區分

1)對於==,比較的是值是否相等

若是做用於基本數據類型的變量,則直接比較其存儲的 值是否相等,

若是做用於引用類型的變量,則比較的是所指向的對象的地址是否相等。

其實==比較的不論是基本數據類型,仍是引用數據類型的變量,比較的都是值,只是引用類型變量存的值是對象的地址

2)對於equals方法,比較的是是不是同一個對象

首先,equals()方法不能做用於基本數據類型的變量,

另外,equals()方法存在於Object類中,而Object類是全部類的直接或間接父類,因此說全部類中的equals()方法都繼承自Object類,在沒有重寫equals()方法的類中,調用equals()方法其實和使用==的效果同樣,也是比較的是引用類型的變量所指向的對象的地址,不過,Java提供的類中,有些類都重寫了equals()方法,重寫後的equals()方法通常都是比較兩個對象的值,好比String類。

Object類equals()方法源碼:

public boolean equals(Object obj) {
     return (this == obj);
}
複製代碼

String類equals()方法源碼:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}
複製代碼

3.2 示例

示例1:

int x = 10;
int y = 10;
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(x == y); // true
System.out.println(str1 == str2); // false
System.out.println(str1.equals(str2)); // true
複製代碼

示例2:

String str3 = "abc";
String str4 = "abc";
System.out.println(str3 == str4); // true
複製代碼

str3與str4相等的緣由是用到了內存中的常量池,當運行到str3建立對象時,若是常量池中沒有,就在常量池中建立一個對象"abc",第二次建立的時候,就直接使用,因此兩次建立的對象實際上是同一個對象,它們的地址值相等。

示例3:

先定義學生Student類

package com.zwwhnly.springbootaction;

public class Student {
    private int age;

    public Student(int age) {
        this.age = age;
    }
}
複製代碼

而後建立兩個Student實例來比較

Student student1 = new Student(23);
Student student2 = new Student(23);

System.out.println(student1.equals(student2)); // false
複製代碼

此時equals方法調用的是基類Object類的equals()方法,也就是==比較,因此返回false。

而後咱們重寫下equals()方法,只要兩個學生的年齡相同,就認爲是同一個學生。

package com.zwwhnly.springbootaction;

public class Student {
    private int age;

    public Student(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        Student student = (Student) obj;
        return this.age == student.age;
    }
}
複製代碼

此時再比較剛剛的兩個實例,返回true。

Student student1 = new Student(23);
Student student2 = new Student(23);

System.out.println(student1.equals(student2)); // true
複製代碼

4. String,StringBuilder,StringBuffer的區別

4.1 區別講解

1)運行速度

運行速度快慢順序爲:StringBuilder > StringBuffer > String

String最慢的緣由:

String爲字符串常量,而StringBuilder和StringBuffer均爲字符串變量,即String對象一旦建立以後該對象是不能夠更改的,但後二者的對象是變量,是能夠更改的。

2)線程安全

在線程安全上,StringBuilder是線程不安全的,而StringBuffer是線程安全的(不少方法帶有synchronized關鍵字)。

3)使用場景

String:適用於少許的字符串操做的狀況。

StringBuilder:適用於單線程下在字符緩衝區進行大量操做的狀況。

StringBuffer:適用於多線程下在字符緩衝區進行大量操做的狀況。

4.2 示例

以拼接10000次字符串爲例,咱們看下三者各自須要的時間:

String str = "";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
    str = str + i;
}
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
System.out.println("String消耗時間:" + time);

StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for (int j = 0; j < 10000; j++) {
    builder.append(j);
}
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.println("StringBuilder消耗時間:" + time);

StringBuffer buffer = new StringBuffer("");
startTime = System.currentTimeMillis();
for (int k = 0; k < 10000; k++) {
    buffer.append(k);
}
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.println("StringBuffer消耗時間:" + time);
複製代碼

運行結果:

String消耗時間:258

StringBuilder消耗時間:0

StringBuffer消耗時間:1

也驗證了上面所說的StringBuilder > StringBuffer > String。

5. 裝箱和拆箱

5.1 什麼是裝箱?什麼是拆箱?

裝箱:自動將基本數據類型轉換爲包裝器類型。

拆箱:自動將包裝器類型轉換爲基本數據類型。

Integer i = 10; // 裝箱
int j = i; // 拆箱
複製代碼

5.2 裝箱和拆箱是如何實現的?

裝箱過程是經過調用包裝器的valueOf方法實現的,

而拆箱過程是經過調用包裝器實例的xxxValue方法實現的(xxx表明對應的基本數據類型)。

怎麼證實這個結論呢,咱們新建個Main類,在主方法中添加以下代碼:

package com.zwwhnly.springbootaction;

public class Main {
    public static void main(String[] args) {
        Integer i = 100;        
        int j = i;
    }
}
複製代碼

而後打開cmd窗口,切換到Main類所在路徑,執行命令:javac Main.java,會發現該目錄會生成一個Main.class文件,用IDEA打開,會發現編譯後的代碼以下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zwwhnly.springbootaction;

public class Main {
    public Main() {
    }

    public static void main(String[] var0) {
        Integer var1 = Integer.valueOf(100);
        int var2 = var1.intValue();
    }
}
複製代碼

注意事項:以上所講使用的是IDEA 2017.2

而在較新版本的IDEA(2018.3.3或者2019.1.3),看到的Main.class是下面這樣的:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zwwhnly.springbootaction;

public class Main {
    public Main() {
    }

    public static void main(String[] var0) {
        Integer var1 = 100;
        int var2 = var1;
    }
}
複製代碼

5.3 示例

示例1:

Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;

System.out.println(i1==i2);
System.out.println(i3==i4);
複製代碼

輸出結果:

false

false

爲何都返回false呢,咱們看下Double.valueOf()方法,就知曉了:

private final double value;

public Double(double value) {
   this.value = value;
}

public static Double valueOf(double d) {
   return new Double(d);
}
複製代碼

示例2:

Boolean i1 = false;
Boolean i2 = false;
Boolean i3 = true;
Boolean i4 = true;

System.out.println(i1==i2);
System.out.println(i3==i4);
複製代碼

輸出結果:

true

true

爲何都返回true呢,咱們看下Boolean.valueOf()方法,就知曉了:

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);

public static Boolean valueOf(boolean b) {
   return (b ? TRUE : FALSE);
}
複製代碼

6. Java中的值傳遞和引用傳遞

6.1 基本概念

值傳遞:傳遞對象的一個副本,即便副本被改變,也不會影響源對象,由於值傳遞的時候,其實是將實參的值複製一份給形參。

引用傳遞:傳遞的並非實際的對象,而是對象的引用,外部對引用對象的改變也會反映到源對象上,由於引用傳遞的時候,其實是將實參的地址值複製一份給形參。

說明:對象傳遞(數組、類、接口)是引用傳遞,原始類型數據(整形、浮點型、字符型、布爾型)傳遞是值傳遞。

6.2 示例

示例1(值傳遞):

package com.zwwhnly.springbootaction;

public class ArrayListDemo {

    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;

        swap(num1, num2);

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
    }

    public static void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;

        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}
複製代碼

運行結果:

a = 20

b = 10

num1 = 10

num2 = 20

能夠看出,雖然在swap()方法中a,b的值作了交換,可是主方法中num1,num2的值並未改變。

示例2(引用類型傳遞):

package com.zwwhnly.springbootaction;

public class ArrayListDemo {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};

        change(arr);

        System.out.println(arr[0]);
    }

    public static void change(int[] array) {
        System.out.println(array[0]);
        array[0] = 0;
    }
}
複製代碼

運行結果:

1

0

能夠看出,在change()方法中將數組的第一個元素改成0,主方法中數組的第一個元素也跟着變爲0。

示例3(StringBuffer類型):

package com.zwwhnly.springbootaction;

public class ArrayListDemo {

    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("博客園:申城異鄉人");
        System.out.println(stringBuffer);

        changeStringBuffer(stringBuffer);
        System.out.println(stringBuffer);
    }

    public static void changeStringBuffer(StringBuffer stringBuffer) {
        stringBuffer = new StringBuffer("掘金:申城異鄉人");

        stringBuffer.append(",歡迎你們關注");
    }
}
複製代碼

運行結果:

博客園:申城異鄉人

博客園:申城異鄉人

也許你會認爲第2次應該輸出「掘金:申城異鄉人,歡迎你們關注」,怎麼輸出的仍是原來的值呢,那是由於在changeStringBuffer中,又new了一個StringBuffer對象,此時stringBuffer變量指向的內存地址已經改變,因此主方法中的stringBuffer變量未受到影響。

若是修改changeStringBuffer()方法的代碼爲:

public static void changeStringBuffer(StringBuffer stringBuffer) {

    stringBuffer.append(",歡迎你們關注");
}
複製代碼

則運行結果會變爲:

博客園:申城異鄉人

博客園:申城異鄉人,歡迎你們關注

示例4(String類型):

package com.zwwhnly.springbootaction;

public class ArrayListDemo {

    public static void main(String[] args) {
        String str = new String("博客園:申城異鄉人");
        System.out.println(str);

        changeString(str);
        System.out.println(str);
    }

    public static void changeString(String string) {
        //string = "掘金:申城異鄉人";
        string = new String("掘金:申城異鄉人");
    }
}
複製代碼

運行結果:

博客園:申城異鄉人

博客園:申城異鄉人

在changeString()方法中無論用string = "掘金:申城異鄉人";仍是string = new String("掘金:申城異鄉人");,主方法中的str變量都不會受影響,也驗證了String建立以後是不可變動的。

示例5(自定義類型):

package com.zwwhnly.springbootaction;

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(String name) {
        this.name = name;
    }
}
複製代碼
package com.zwwhnly.springbootaction;

public class ArrayListDemo {

    public static void main(String[] args) {
        Person person = new Person("zhangsan");
        System.out.println(person.getName());

        changePerson(person);
        System.out.println(person.getName());
    }

    public static void changePerson(Person p) {
        Person person = new Person("lisi");
        p = person;
    }
}
複製代碼

運行結果:

zhangsan

zhangsan

修改changePerson()方法代碼爲:

public static void changePerson(Person p) {
    p.setName("lisi");
}
複製代碼

則運行結果爲:

zhangsan

lisi

7. 參考

關於==和equals的區別和聯繫,面試這麼回答就能夠

Java中==號與equals()方法的區別

Java中的String,StringBuilder,StringBuffer三者的區別

深刻剖析Java中的裝箱和拆箱

Integer、new Integer() 和 int 比較的面試題

java面試題之int和Integer的區別

最最最多見的Java面試題總結-第一週

8. 最後

打個小廣告,歡迎掃碼關注微信公衆號:「申城異鄉人」,按期分享Java技術乾貨,讓咱們一塊兒進步。

相關文章
相關標籤/搜索