《Cracking the Coding Interview》——第14章:Java——题目3

2014-04-26 18:59

题目:final、finally、finalize有什么区别?

解法:烂大街之java语法题。此题被多少公司考过我不知道,反正我确实遇见过一次了。

代码:

 1 // 14.3 final, finally and finalize, what are they?

 2 // you can't inherit me

 3 public final class TestJava {

 4     // you can't modify me

 5     public final int i = 123;

 6     public final int j;

 7     

 8     public TestJava () {

 9         j = 2;

10         // you can't modify me again.

11         // j = 3;

12     }

13     

14     // you can't override me

15     public final void f() {

16         System.out.println("TestJava.f()");

17     };

18     

19     // a substitute for ~TestJava().

20     // it signifies it is ready to be collected, but it doesn't have to be destroyed immediately.

21     @ Override

22     protected void finalize() {

23         System.out.println("finalized");

24     };

25     

26     public static void main(String[] args) {

27         TestJava testJava = new TestJava();

28         testJava.f();

29         testJava.finalize();

30     }

31 }

 

你可能感兴趣的:(interview)