對java中一些關鍵的,容易迷惑的知識點的歸納總結

來源:互聯網
上載者:User
 

一.  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;}

問題點數:100 回複次數:149

顯示所有回複顯示星級回複顯示樓主回複

修改
刪除
舉報
引用
回複


加為好友
發送私信
線上聊天
  • myJavaRoad
  • 置心一處,無事不辦
  • 等級:
  • 可用分等級:貧農
  • 總技術分:3
  • 總技術分排名:294445
發表於:2009-03-27 14:54:371樓 得分:0
調整了半天還是不理想,明明發之前調整好了,一發就變了樣

六.java類中的變數初始化相關的知識:6-1.初始化順序分三步:
1.類載入時,初始化靜態變數和靜態區塊,先父類後子類
2.運行中當new出一個對象時,開始為對象分配空間並初始化執行個體變數,先父類後子類
3.調用建構函式時,先執行父類的建構函式,再執行子類的建構函式,具體過程是調用子類的建構函式時,在第一行處會調用父類的建構函式(顯式或隱式)

6-2. 初始化時各類型的變數初始化的値:
應用類型: null
基本類型: boolean : false
          Char:/u0000
          Byte: 0
          Short: 0
          Int: 0
          Long: 0
          Float: 0.0
          Double: 0.0

    6-3. 數組的初始化
         
當我們產生某個儲存物件的數組時,真正產生的其實是個儲存references的數組。此數組建立之後,其中的每一個reference皆會被自動設為某
個特殊值。該值以關鍵字null表示。當Java看到null值,便將這個reference視為“不指向任何對象”。使用任何reference之前,
你必須先將某個對象指派給它。如果你使用某個reference而其值為null,便會在執行期發生錯誤
        數組在分配空間時就開始了初始化,初始化規則,基本類型按照6-2的規則進行初始化,應用類型類型全部初始化為null
執行個體1 :
int index = 1;
int [] foo = new int [3];
int bar = foo [index];
int baz = bar + index;

What is the result?
A.baz has the value of 0
B.baz has the value of 1
C.baz has the value of 2
D.an exception is thrown
E.the code will not compile

執行個體2:
1.String foo = “blue”;
2.Boolean[]bar = new Boolean [1];
3.if (bar[0]) {
4.  foo = “green”;
5.}

What is the result?

A.foo has the value of “”
B.foo has the value of null
C.foo has the value of “blue”
D.foo has the value of “green”
E.an exception is thrown
F.the code will not compile

      6-4. java中的所有的執行個體變數都有系統預設初始化,所有的方法變數由方法本身進行初始化,且方法中的變數一定要初始化後才能應用
例題:
  class Parent {       
    // 靜態變數       
    public static String p_StaticField = "父類--靜態變數";       
    // 變數       
    public String p_Field = "父類--變數";       
     
    // 靜態初始化塊       
    static {       
        System.out.println(p_StaticField);       
        System.out.println("父類--靜態初始化塊");       
    }       
     
    // 初始化塊       
    {       
        System.out.println(p_Field);       
        System.out.println("父類--初始化塊");       
    }       
     
    // 構造器       
    public Parent() {       
        System.out.println("父類--構造器");       
    }       
}       
     
public class SubClass extends Parent {       
    // 靜態變數       
    public static String s_StaticField = "子類--靜態變數";       
    // 變數       
    public String s_Field = "子類--變數";       
    // 靜態初始化塊       
    static {       
        System.out.println(s_StaticField);       
        System.out.println("子類--靜態初始化塊");       
    }       
    // 初始化塊       
    {       
        System.out.println(s_Field);       
        System.out.println("子類--初始化塊");       
    }       
     
    // 構造器       
    public SubClass() {       
        System.out.println("子類--構造器");       
    }       
     
    // 程式入口       
    public static void main(String[] args) {       
        new SubClass();       
    }       

 
七.java中的建構函式
1.建構函式不能被繼承
2.每一個類都至少有一個建構函式,自己不定義,編譯器也會給分配一個預設的不帶參數的建構函式
3. 子類的建構函式一定會調用父類的建構函式,通過super()調用,或顯式或隱式,顯式調用的父類建構函式必須存在;
如果沒有顯式調用則編譯器會自動在子類的建構函式第一行處加上super()這個隱式調用,這時要求父類一定要有不帶參數的建構函式存在(如果父類自己定
義了建構函式,但帶有參數,編譯時間會報錯)
例子:
class super1{
public int I = 0;
public super1 (String text){
I = 1;
}
}
public class sub1 extends super1{
public sub1(String text){
  // super(text);
I= 2;
//隱式超級構造super1()是未定義的。必須明確援引另一個構造
}
public static void main (String args[]){
sub1 sub2 = new sub1("Hello");
System.out.println(sub2.I);
}
}

八.java中的異常處理
1. java中的異常分運行時異常 和 非運行時異常, 運行時異常由運行時系統捕獲並處理(編譯正常),非運行時異常必須由處理(拋出或捕獲)

2. 異常機制中try{}後一定要跟catch嗎?
* 不一定,,但必須跟finally.也就是catch和finally必須跟其中一個
* 異常機制中try{}後一定要跟catch嗎?
* 不一定,,但必須跟finally.也就是catch和finally必須跟其中一個
*  try {     
*  }finally {}
* 這樣沒問題,而且,可不是沒有意義哦,因為這樣可以保證即使發生了異常,finally裡面的代碼一定會被執行。
* 有時候,這個還是非常有用的。
* 比如可以用來釋放一些自己佔用的資源,然後讓調用者處理異常。
  3.  異常中的finally一定會執行,哪怕一個方法中有return語句,也是在異常處理後才返回
  4.  異常的拋出可以先子類再父類,如果子類捕獲了,則父類就不再捕獲;
但是不能先父類再子類,那樣會導致編譯出錯
  5.  異常處理後,程式繼續執行
執行個體:
/*
* 非運行時異常一旦拋出,要麼用catch塊捕獲處理,要麼聲明拋出
*/
import java.io.IOException;
public class ExceptionTest{
//public static void methodA(){
public static void methodA() throws IOException{
//throw new NullPointerException();
//try{
throw new IOException();
//System.out.println("method exit");
//}catch(IOException e){}
//finally{}
}
public static void main (String[] args){
try {
methodA();
//throw new IOException();
} catch (IOException e)  { System.out.println("Caught1 IOException ");
} catch (NullPointerException e)  {
System.out.println("Caught1 NullPointerException");
} catch (Exception e)  {
System.out.println("Caught Exception");
}

System.out.println("main exit");
}
}

What is the output?
Ans:

九. 按位元運算和邏輯運算
    按位元運算操作符(& ,| )兩邊的都要計算
    邏輯運算如果操作符(&&, || )左邊成立則就不在計算右邊了

執行個體:
1.public class test{   
2.    private static int j = 0;     
4.    private static boolean methodB(int k) {
5.        j += k;
6.        return true;
7.}
9.    public static void methodA(int  i) {
10.        boolean b: 
11.        b = i < 10 | methodB (4);
12.        b = i < 10 || methodB (8);
15.    public static void main (String args[] ) {
16.        methodA (0);
17.        System.out.println(j);
18.  }
19}

What is the result?
A.The program prints “0”
B.The program prints “4”
C.The program prints “8”
D.The program prints “12”
E.The code does not complete


修改
刪除
舉報
引用
回複


加為好友
發送私信
線上聊天
  • myJavaRoad
  • 置心一處,無事不辦
  • 等級:
  • 可用分等級:貧農
  • 總技術分:3
  • 總技術分排名:294445
發表於:2009-03-27 15:45:032樓 得分:0
十.for(;;)意義
相當於while(true), 不知道java為什麼要搞出這個古怪讓人費解的東西?

十一.equals, = =
  equals比較兩個對象的內容是否相等
    = = 比較的是兩個引用是否指向同一對象    String的儲存特性會對以上的判定規則產生影響:
 
String 通過“”產生的對象會儲存在常量池中,常量池有一個很重要的特點就是能共用,比如String s = “X”;
在把”X”放常量池之前jvm會檢測常量池中是否存在相同的對象,如果已經存在則直接把引用指向已存在的對象,不再為”X”分配空間,好處是節約了空間
執行個體:
  String s1 = “ABC”;
  String s2 = “ABC”;
  以下各結果為true, 還是為false
  S1 == s2;  //true ,String的特性決定的
  S1.equals(s2);  //true

Jdk1.5後引入了自動打包自動解包的功能,對以上的判定規則也會產生影響:比如以下是正確的定義
  Double d = 1.0;  //java編譯器會自動把1.0打包成New Double(1.0);
執行個體:
Integer i = new Integer (42);
Long l  = new Long (42);
Double d = new Double (42.0);

Which one expressions evaluate to True?
A.(i == l)
B.(i == d)
C.(d == l)
D.(i.equals (d))
E.(d.equals (i))
F.(i.equals (42))  //42會自動打包成new Integer(42)

十二. 基本類型的變數賦初始値
  Byte的範圍為-128~127
  當我們給出一個整數,且該整數後不帶l標示,則編譯器自動把它視為int類型,如
      Int i = 1 ; 是成立的
  當我們給出一個小數,且該小數後不帶f標示,則編譯器自動把它視為double類型,如
      Double d = 1.0; 是成立的

十三. 基本類型的轉化
  規則: 小的可以自動轉化為大的, 大的要強制性才能轉為小的,比如以下
      Double d = 1.0f;  //正確, 小轉大,自動
      Float f  = 1.0d(或1.0);  //錯誤,大轉小,需強制 float f = (float)1.0d;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.