這些坑你一定填過!

前言

金九銀十招聘季,正是求職面試的時候。俗話說機會都是留給有準備的人的,所以無論是有過開發經驗還是剛剛走出校門的應屆生,面對機會也是要提前準備的。HR的面試我們就不說了,專業面試一般分爲筆試和上機。但是大部分是筆試,要求比較高的可能會讓你上機。當然這也是要看公司的。

欲善其事,先利其器

  1. 請問下面輸出的結果是什麼?
public class demo01 {
     public static void main(String[] args) {
          String strBerPath = "demo.wx.";
          String strRearPath = "demo01.class";
          String classFile = strBerPath. replaceAll(".", "/") + strRearPath;
          System.out.println(classFile);
      }
}

結果:

A. demo.wx
B. demo/wx/demo01.class
C. ////////demo01.class
D. demo.wx.demo01

看到這裏你有答案了嗎?正確答案是C

replaceAll()方法使用給定的參數替換字符串所有匹配給定的正則表達式的子字符串。第一個參數表示,匹配此字符串的正則表達式。第二個參數表示,用來替換每個匹配項的字符串。而".“在正則表達式中表示任何字符,所以會把前面字符串的所有字符都替換成”/"。如果想替換的只是".",那麼就要寫成"\."

從新寫一遍,試試看

public static void main(String[] args) {
	String strBerPath = "demo.wx.";
	String strRearPath = "demo01.class";
	String classFile = strBerPath. replaceAll("\\.", "/") + strRearPath;
	System.out.println(classFile);
}

運行結果:
在這裏插入圖片描述
是不是感覺第一題很簡單?彆着急這只是熱熱身,坑還在後面。

  1. 請給出下面的結果
public class demo02 {

    public static void main(String[] args) {
        switch (num) {
            case 01:
                System.out.println("星期一");

            case 02:
                System.out.println("星期二");

            case 03:
                System.out.println("星期三");

            case 04:
                System.out.println("星期四");

            case 05:
                System.out.println("星期五");

            default:
                System.out.println("休息");
        }
    }
}

當num=01的時候輸出什麼結果?正確答案是:星期一 星期二 星期三 星期四 星期五 休息

jdk7之前switch表達式只接收int的兼容類型(byte 、char 、short 、 int )。之後多加了一個String類型。switch函數的工作原理是,找到第一個滿足條件的結果後,輸出後續全部結果。若想找到匹配的結果後不再執行後續,可以加入break;跳出函數代碼塊。

  1. 包裝類,給出下面結果:
public class demo03 {
    public static void main(String[] args) {
       Integer i = 127;
       Integer j = 127;
        System.out.println(i == j);
    }
}

正確答案true

這題應該沒什麼問題,那麼請看這一題:

public static void main(String[] args) {
       Integer i = 128;
       Integer j = 128;
        System.out.println(i == j);
    }

正確答案false

仔細看看這兩道題到底哪裏不一樣?爲什麼一個是true,一個是false呢?原因在於Integer存在一個範圍(-128-127),如果超出了範圍,會從堆區new一個Integer對象來存放值,所以結果爲false。Integer是直接賦值,且在範圍內,所以是在常量池中開闢出同一個空間來存放127,i、j都指向127。

  1. 數組轉換爲List
public class demo04 {
    public static void main(String[] args) {
       String[] str = new String[]{"星期一","星期二","星期三","星期四","星期五"};
       List<String> list = Arrays.asList(str);
       list.add("休息");
        System.out.println(list.size());
    }
}

運行結果報錯
在這裏插入圖片描述
第十行對應的代碼是list.add(),是不是有點蒙了?這個怎麼會報錯呢?是因爲調用Arrays的asList()方法將數組轉換成List時返回的是Arrays的靜態內部類ArrayList,它自身並沒有重寫add()方法,而其父類AbstractList實現的add()方法只會拋出UnsupportedOperationException,所以我們調用的是隻會拋出UnsupportedOperationException的AbstractList的add()方法,這就是異常出現的原因了

  1. String類型
public class demo06 {

    String str = new String("good");
    char[] ch = {'a','b','c'};
    public static void main(String[] args) {
        demo06 ex = new demo06();
        ex.change(ex.str, ex.ch);
        System.out.print(ex.str +"and");
        System.out.print(ex.ch);
    }

    public void change(String str, char ch[]){
        str= "test ok";
        ch[0]= 'g';
    }

}

結果:
A. test okandabc
B. test okandgbc
C. goodandabc
D. goodandgbc

正確答案:D

因爲String是一個final修飾的不可變類。str指向"good",在change方法中作爲參數會複製一個副本引用對象傳入,str指向"testok",但是原來的“good”依然存在字符串常量池中。所以str依然指向"good"。char[]數組作爲參數傳入時傳入的是引用傳遞(會改變原來的值)。而String傳遞的是引用的副本(不會改變原來的值)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章