Java面試寶典 1.13~1.12【2020.5 Beta版】

1.Java基礎(chǔ)

1.1 Java基礎(chǔ)部分的順序

Java基礎(chǔ)順序

1.2 Java中有沒有g(shù)oto?

有,但是現(xiàn)在并沒有使用,和const一樣都是保留字。

1.3 談?wù)?& 與 && 的區(qū)別

相同點(diǎn)是都可以做邏輯與的關(guān)系,表示and

不同的是 && 帶有短路功能,從左往右,如果碰到一個(gè)為false,就不會往后判斷執(zhí)行

例如


// A true B false C true

if(A && B && C){}

// 那么 執(zhí)行到B后就停了,不會再去執(zhí)行C

來看一個(gè)面試題


Stirng s = null;

if((s!=null) & (s.length == 0)){}

if((s!=null) && (s.length == 0)){}

第一個(gè)會拋出異常,所以日常判斷null時(shí),看情況大概率要使用 &&

1.4 Java 中如何跳出多重循環(huán)?

  1. 使用帶有標(biāo)號的break語句

public class TestFor {

    public static void main(String[] args) {

        break_status:

        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 10; j++) {

                System.out.println(String.format("i--%d,j--%d", i, j));

                if (j == 1) {

                    break break_status;

                }

            }

        }

    }

}

  1. 外層循環(huán)受到內(nèi)層循環(huán)的條件控制

public class TestArray {

    public static void main(String[] args) {

        int[][] ary = {{1, 2}, {3, 4}, {5, 6}};

        boolean flag = false;

        for (int i = 0; i < ary.length && !flag; i++) {

            for (int j = 0; j < ary[i].length; j++) {

                System.out.println("ary[i][j]:" + ary[i][j]);

                if (ary[i][j] == 3) {

                    flag = true;

                    break;

                }

            }

        }

    }

}

1.5 switch 可以作用在哪些數(shù)據(jù)類型上?

(1) char, Character

(2) byte, Byte

(3) short, Short

(4) int, Integer

(5) String enum(枚舉)

1.6 下述switch 的輸出是什么?


public class TestSwitch {

    public static void main(String[] args) {

        int i = 2;

        switch (i){

            case 1:

                System.out.print(1);

            case 2:

                System.out.print(2);

            case 3:

                System.out.print(3);

            default:

                System.out.print(4);

        }

    }

}

輸出 234 ,匹配到了2,沒有break,所以繼續(xù)運(yùn)行,第一次匹配到了case之后,后面的case就和goto標(biāo)號一樣了,沒什么用繼續(xù)輸出

1.7 short s1=1;s1 =s1+1,有什么錯(cuò)誤?short s1=1;s1+=1;有什么錯(cuò)?

在這里插入圖片描述

1.8 char類型中可以存儲一個(gè)中文漢字嗎?

char是按照字符存儲的,不管英文還是中文,固定占用占用2個(gè)字節(jié),用來儲存Unicode字符

unicode編碼字符集中包含了漢字,所以,char型變量中可以存儲漢字

在這里插入圖片描述

1.9 用最有效的方法算出2*8=?

2 << 3

因?yàn)閷⒁粋€(gè)數(shù)左移n位,就相當(dāng)于乘以了2的n次方,那么,一個(gè)數(shù)乘以8只要將其左移3位即可

1.10 百億計(jì)算器的實(shí)現(xiàn)


public class CalculationUtils {

    // 除法保留小數(shù)位

    private final static int ROUND_SCALE = 6;

    @SafeVarargs

    public static <T extends Number> BigDecimal add(T... arg) {

        if (null != arg) {

            BigDecimal res = BigDecimal.ZERO;

            for (T num : arg) {

                if (null != num) {

                    BigDecimal argTranser = getBigDecimal(num);

                    res = res.add(argTranser);

                }

            }

            return res;       

        } else {

            return BigDecimal.ZERO;

        }

    }

    @SafeVarargs

    public static <T extends Number> BigDecimal div(T... arg) {

        if (null != arg) {

            BigDecimal res = BigDecimal.ZERO;

            for (int i = 0; i < arg.length; i++) {

                BigDecimal argTranser = getBigDecimal(arg[i]);

                if (i == 0) {

                    res = argTranser;

                } else {

                    res = res.divide(argTranser, ROUND_SCALE, RoundingMode.HALF_UP);

                }

            }

            return res;

        } else {

            return BigDecimal.ZERO;

        }

    }

    public static <T extends Number> BigDecimal getBigDecimal(T num) {

        BigDecimal argTranser;

        if (num instanceof Float) {

            argTranser = new BigDecimal(String.valueOf(num));

        } else {

            argTranser = BigDecimal.valueOf(num.doubleValue());

        }

        return argTranser;

    }

    @SafeVarargs

    public static <T extends Number> BigDecimal mul(T... arg) {

        if (null != arg) {

            BigDecimal res = BigDecimal.ZERO;

            for (T num : arg) {

                if (null != num) {

                    BigDecimal argTranser = getBigDecimal(num);

                    res = res.multiply(argTranser);

                }

            }

            return res;

        } else {

            return BigDecimal.ZERO;

        }

    }

    @SafeVarargs

    public static <T extends Number> BigDecimal sub(T... arg) {

        if (null != arg) {

            BigDecimal res = BigDecimal.ZERO;

            for (T num : arg) {

                if (null != num) {

                    BigDecimal argTranser = getBigDecimal(num);

                    res = res.subtract(argTranser);

                }

            }

            return res;

        } else {

            return BigDecimal.ZERO;

        }

    }

}

1.11 使用final關(guān)鍵字修飾一個(gè)變量時(shí),是引用不能變,還是引用的對象不能變?

在這里插入圖片描述

得出

  • 引用不能變

  • 引用變量所指向的對象的內(nèi)容是可以改變

1.12 Java中==和.equals()方法之間的區(qū)別

在這里插入圖片描述

?


public class TestEquals {

    public static void main(String[] args) {

        Integer a = 1;

        Integer b = new Integer(1);

        Integer c = new Integer(1);

        Integer d = 1;

        Integer f = 129;

        Integer g = 129;

        System.out.println(a == d);

        System.out.println(b.equals(c));

        System.out.println(f == g);

    }

}

輸出


true

true

false

jvm在運(yùn)行時(shí)創(chuàng)建了一個(gè)緩存區(qū)域存儲了-128至127的值的數(shù)組,如果integer的值在-128至127之間,則是去緩存里面獲取。因此上面的 a 和 d 指向的是同一個(gè)內(nèi)存地址。

因?yàn)?129 超過了這個(gè)緩存區(qū)域,所以賦值的時(shí)候是新建了兩個(gè)內(nèi)存地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容