一. Switch 1.其能接受的資料類型有四個,char , byte, short, int 2.Default 可放在switch中的任何一個地方,但只有給定的條件匹配不到時,才會執行 3.Case,default語句如果執行完要跳出,必須用break, 沒的話會向下繼續執行(如果碰到case語句則直接進入執行) 執行個體1: 1.int i=1, j=0 3.switch(i){ 4. case 2: 5. j+=6; 7. case 4: 8. j+=1; 10. default: 11. j +=2; 13. case 0: 14. j +=4; 15.} What is the value of j at line 16? A.0 B.1 C.2 D.4 E.6 執行個體2: 1. switch (i) { 2. default: 3. System.out.printIn(“Hello”); 4. } What is the acceptable type for the variable i? A.byte B.long C.float D.double E.object F.A and B G.C and D 二. String 和 StringBuffer String 定義的是字串常量,其値一旦定義就不再改變,如下: String s = “ABC”; S = s.subString(2); //會重建一個字串對象 以上兩句執行後在記憶體中會產生“兩”個字串對象 一個”ABC”,另一個是s指向的”AB”(注意s已不再指向”ABC”) StringBuffer 定義的是字串變數,其値可以改變,如下: StringBuffer s1 = new StringBuffer(“ABC”); S1 = s1.subString(2); 以上兩句執行後在記憶體中只產生“一個”字串對象: s指向的”AB”; 執行個體1: 1.public class Foo { 2. public static void main (String [] args){ 3. StringBuffer a = new StringBuffer (“A”); 4. StringBuffer b = new StringBuffer (“B”); 5. operate (a,b); 6. system.out.printIn{a + “,” +b}; 7.} 8. static void operate (StringBuffer x, StringBuffer y) { 9. x.append {y}; 10. y = x; 11. ) 12.} What is the output? Ans: 執行個體2: 1.Public class test{ 2.Public static void stringReplace (String text){ 3. Text = text.replace (‘j’ , ‘i’); 4.} 5. 6.public static void bufferReplace (StringBuffer text) { 7. text = text.append (“C”) 8.} 9. 10.public static void main (String args[]) { 11. String textString = new String (“java”); 12. StringBuffer textBuffer = new StringBuffer (“java”); 13. 14. stringReplace (textString); 15. BufferReplace (textBuffer); 16. 17. System.out.printIn (textString + textBuffer); 18. } 19. } What is the output? Ans: 三. String s = new String(“XYZ”); 該語句會產生2個字串對象: 一個是通過 ” ” 方式在 編譯期 產生,存放在常量池中 一個是通過new方式在 運行期 產生,存放在堆記憶體中 但在運行時只會通過new方式產生一個對象 四. java中的參數只能“按値”傳遞,且傳遞的是値的 copy 如是基本類型,則傳遞的是基本類型的副本 如是參考型別,則傳遞的是引用本身的副本 參見2的執行個體 五. 方法重載和覆蓋的條件 符合重載的條件: 1.在同一個類中 2.有多個同名的方法, 3.方法參數不同(參數的個數不同 或則 參數的類型不同) 執行個體: 1.public class MethodOver { 2. public void setVar (int a, int b, float c) { 3. } 4.} Which two overload the setVar method? (Choose Two) A.private void setVar (int a, float c, int b) { } B.protected void setVar (int a, int b, float c) { } C.public int setVar (int a, float c, int b) (return a;) D.public int setVar (int a, int b, float c) (return a;) E.protected float setVar (int a, int b, float c) (return c;) 符合覆蓋的條件: 1.在繼承中 2.子類中的方法名和父類相同 3.子類中的方法參數和父類相同 4.子類中的方法傳回型別和父類一樣 5.子類的方法不能比父類拋出更多的異常 6.子類的方法存取範圍大於或等於父類 覆蓋值得注意的是如果子類中有一個方法名稱和父類一樣,但參數不同,那不叫覆蓋,所以也就不受覆蓋的條件限制(注意該方法可以存在) 執行個體: 1.class BaseClass { 2. Private float x = 1.0f ; 3. protected float getVar ( ) ( return x;) 4.} 5.class Subclass extends BaseClass ( 6. private float x = 2.0f; 7. //insert code here 8.) Which two are valid examples of method overriding? (Choose Two) A.float getVar ( ) { return x;} B.public float getVar ( ) { return x;} C.float double getVar ( ) { return x;} D.protected float getVar ( ) { return x;} E.public float getVar (float f ) { return f;} |