java基礎-001,java-001

來源:互聯網
上載者:User

java基礎-001,java-001

一、區分final 、 finally 、 finalize 

1.關鍵字final

  Java語言的關鍵字final可以用於變數、類或方法,但是含義會有所不同。

  -用於變數:一旦初始化,變數值就不能修改

  -用於方法:該方法不能被子類重寫(overrride)

  -用於類:該類不能派生出子類

2.關鍵字finally

  關鍵字finally和try/catch語句配對使用,即使有異常拋出,也能確保某段代碼一定會執行。finally語句塊會在try/catch語句塊之後,在控制權交回之前執行。

3.finalize方法

  在真正銷毀對象之前,自動垃圾收集器會調用finalize()方法。因此,一個類可以重寫Object類的finalize()方法,以便定義在垃圾收集時的特定行為。

  

protected void finalize() throws Throwable{      /*關閉以開啟的檔案,釋放資源等操作*/  }

  

二、區分重載與重寫

1.重載(overloading)是指兩個方法的名稱相同,但參數類型或個數不同。

1 1 public double computeArea(Circle c ){...........}2 3 2 public double computeArea(Square s){..........} 

2.重寫(overriding)是指某個方法與父類的方法擁有相同的名稱和函數簽名

三、常用集合架構

1.ArrayList:ArrayList是一種可動態調整大小的數組,隨著元素的插入,數組會適時擴容。

1 ArrayList<String> myArray = new ArrayList<String>();2 myArray.add("one");3 myArray.add("two");4 System.out.println(myArray.get(0)); //列印“one”

2.Vector:vector與ArrayList非常相似,只不過前者(Vector)是同步的(synchronized)。兩者文法也相差無幾。

1 Vector<String> myVector = new Vector<String>();2 myVector.add("one");3 myVector.add("two");4 System.out.println(myVector.get(0));

3.LinkedList:Java內建的LinkedList類,使用時會引出一些迭代器的文法

1 LinkedList<String>  myLinkedList = new LinkedList<String>();2 myLinkedList.add("two");3 myLinkedList.add("one");4 Iterator<String> iter = myLinkedList.iterator();5 while(iter.hasNext()){6       System.out.println( iter.next());      7 }

4.HashMap:HashMap集合廣泛用於各種場合。

1 HashMap<String,String> map = new HashMap<String,String>();2 map.put("one","one");3 map.put("two","two");4 System.out.println(map.get("one"));

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.