package com.heima.test;
public class Test1 {
/**
* * A:面試題1
* final,finally和finalize的區別
* final可以修飾類,不能被繼承
* 修飾方法,不能被重寫
* 修飾變量,只能賦值一次
*
* finally是try語句中的一個語句體,不能單獨使用,用來釋放資源
*
* finalize是一個方法,當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。
* B:面試題2
* 如果catch里面有return語句,請問finally的代碼還會執行嗎?如果會,請問是在return前還是return后。
*/
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.method());
}
* * A:面試題1
* final,finally和finalize的區別
* final可以修飾類,不能被繼承
* 修飾方法,不能被重寫
* 修飾變量,只能賦值一次
*
* finally是try語句中的一個語句體,不能單獨使用,用來釋放資源
*
* finalize是一個方法,當垃圾回收器確定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。
* B:面試題2
* 如果catch里面有return語句,請問finally的代碼還會執行嗎?如果會,請問是在return前還是return后。
*/
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.method());
}
}
class Demo {
public int method() {
int x = 10;
try {
x = 20;
System.out.println(1/0);
return x;
} catch (Exception e) {
x = 30;
return x;
} finally {
x = 40;
//return x; 千萬不要在finally里面寫返回語句,因為finally的作用是為了釋放資源,是肯定會執行的
//如果在這里面寫返回語句,那么try和catch的結果都會被改變,所以這么寫就是犯罪
}
}
}
public int method() {
int x = 10;
try {
x = 20;
System.out.println(1/0);
return x;
} catch (Exception e) {
x = 30;
return x;
} finally {
x = 40;
//return x; 千萬不要在finally里面寫返回語句,因為finally的作用是為了釋放資源,是肯定會執行的
//如果在這里面寫返回語句,那么try和catch的結果都會被改變,所以這么寫就是犯罪
}
}
}