Câu hỏi phỏng vấn Java
Câu hỏi

What is the difference between final , finalize and finally in java?

Câu trả lời

The terms "final," "finally," and "finalize" in Java are often confused due to their similar spelling, but they serve very distinct purposes within the language.

Final Keyword

The final keyword is a modifier in Java that can be applied to classes, methods, and variables, each with different implications:

  • Final Variables: Once a variable is initialized with the final keyword, its value cannot be changed. For example, final int i = 10; means the value of i cannot be altered, and attempting to do so will result in a compile-time error[2][4].
  • Final Methods: A method declared as final cannot be overridden by subclasses. This is used to prevent a method from being modified in any subclass[2][4].
  • Final Classes: When a class is declared as final, it cannot be subclassed. This is useful when you want to prevent the class from being extended to alter its behavior[2][4].

Finally Block

The finally block is used in conjunction with try-catch blocks for exception handling in Java. It is executed after the try and catch blocks have completed and is executed regardless of whether an exception was thrown or caught. This block is typically used for cleanup activities, such as releasing resources like I/O streams or database connections, ensuring that these resources are closed properly even if an error occurs during the execution of the try block[1][2][4].

Finalize Method

The finalize() method is a protected method of the `java.lang.Ob...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What are Directives?

junior

What is Function Overriding and Overloading in Java?

middle

What is difference between ArrayList and LinkedList ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào