java基礎之final、finally和finalize的區別
在java 基礎中,final、finally和finalize的區別有哪些?下面本站小編帶大家一起來看看詳細內容,希望對大家有所幫助!想了解更多相關信息請持續關注我們應屆畢業生考試網!
l可以修飾類,不能被繼承;可以修飾方法,不能被重寫;可以修飾變量,只能賦值一次。
lly是try語句中的語句體,不能單獨使用,用來釋放資源;
lize是一個方法,當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。
如下代碼程序:
package ;
/**
* 面試題1:final,finally和finalize的區別:
*
* final可以修飾類,不能被繼承;
* 可以修飾方法,不能被重寫
* 可以修飾變量,只能賦值一次。
* finally是try語句中的語句體,不能單獨使用,用來釋放資源;
* finalize是一個方法,當垃圾回收器確定不存在對該對象的更多引用時,由對象
* 的垃圾回收器調用此方法。
*
*
* 面試題2:如果catch裏面有return語句,請問finally的代碼還會被執行嗎?
* 如果會,請問是在return前還是在return後?
* 會。在return前。
*
* @author Rocky
*
*/
public class FinallyDemo8 {
public static void main(String[] args) {
Demo d = new Demo();
tln(od());
}
}
class Demo {
public int method() {
int x = 10;
try {
x = 20;
tln(1/0);
return x;
} catch (Exception e) {
x = 30;
return x;
} finally {
/*
* finally中不應當有return語句
* 否則該方法一定會返回這裏的內容
*/
x = 40;
return 40;
//千萬不要在finally裏面寫返回語句,因爲finally的`作用是
//釋放資源,是肯定會執行的,如果在這裏面寫返回語句,那麼try和cache
//的結果都會被改變,所以這麼寫就是犯罪!!!
}
}
}
10Java中HashMap和TreeMap的區別深
相關文章
-
java的String=a; a==null和a.equals(null)這兩個判斷有什麼區別
總結是對某一特定時間段內的學習和工作生活等表現情況加以回顧和分析的一種書面材料,它在我們的學習、工作中起到呈上啓下的作用,不如靜下心來好好寫寫總結吧。我們該怎麼去寫總結呢?以下是小編精心整理的家長學校學期工 -
A Fairy Tale in Fall作文點評
A Fairy Tale in FallRoaming on the path among the trees, I threw my sentiment into the blue sky, which landed on the ground of my Alma Mater,and brought me back to the day three years ago。That was a day -
英語作文International Day of Peace Vigil
The goal of the Inteational Day of Peace Vigil is: "To encourage the observation of a worldwide, grassroots 24-hour vigil for peace and nonviolence on the Inteational Day of Peace, Sunday, 21 Septembe -
win7系統重裝win10提示not a valid image file
我們在使用u啓動u盤重裝win10時,在安裝操作過程中如果彈出not a valid image file錯誤提示,導致系統無法正常安裝,在使用一鍵自動還原系統、手動ghost以及u啓動PE一鍵裝機工具等都解決不了問題時該怎麼辦呢?今天本站小編 -
Family life in China英語作文
I live with my father and mother in a very big house in Dongguan. My parents are both very busy. they leave for work very early in the moing and don’t retu home until late in the evening.When I am ho -
英語閱讀:African Union Leaders Talk Mali, Chinese Investmen
African leaders met in Ethiopia during the weekend to celebrate the 50th anniversary of their continental alliance and discuss issues such as Chinese investment on the continent as well as the fight a -
Liu Xiang in a TV Commercial Film英語美文
高二地理教師期末總結報告 篇1一:學情分析:在高二地理文理教學中,這學期我擔任高二四個文科班的地理教學工作,其中11班基礎較好,8、9、10班的基礎較差。其中的一個原因是國中和基礎比較薄弱,另外一個是他們的地理學習習慣 -
A Festive Lantern Festival fifteen day英語作文
there is a good old saying: "A Festive Lante Festival fifteenth day." This remark vividly reflect the characteristics of the largest Lante Festival, right, is the Lante Festival relatives and friends -
英譯漢文章Life in a violin Case雙語閱讀
錢固然是好東西,但是爲了錢而付出的代價往往太高昂了。下面是小編整理的英譯漢文章Life in a violin Case雙語版本,歡迎閱讀! Life in a violin Case 琴匣子中的生趣Alexander Bloch亞歷山大·布洛克In ord -
Java中finally的問題
引導語:finally是終極的,那麼Java中的lly又是怎樣的呢,以下是本站小編分享給大家的Java中finally的問題,歡迎閱讀! 一個比較繞的例子:try {try {try {String s = null;At(0);} catch(NullPointerException nu) {tln(nu)