Skip to content

Commit 53e24f8

Browse files
committed
2 parents 3b35f31 + 6633569 commit 53e24f8

1 file changed

Lines changed: 45 additions & 4 deletions

File tree

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Frequently asked Java Interview questions
2626
| 17 | [What is finalize method? How do you override it?](#what-is-finalize-method-how-do-you-override-it) |
2727
| 18 | [What Is the Difference Between the Comparable and Comparator Interfaces](#what-is-the-difference-between-the-comparable-and-comparator-interfaces) |
2828
| 19 | [What Is an inner class](#what-is-an-inner-class) |
29+
| 20 | [What is the difference between final, finally, and finalize() in Java?](#what-is-the-difference-between-final-finally-and-finalize-in-java) |
2930
<!-- TOC_END -->
3031

3132
<!-- QUESTIONS_START -->
@@ -683,13 +684,53 @@ Frequently asked Java Interview questions
683684

684685
**[⬆ Back to Top](#table-of-contents)**
685686

686-
20. ### What is the difference between '==' and equals() method?
687+
20. ### What is the difference between final, finally, and finalize() in Java?
688+
689+
The terms `final`, `finally`, and `finalize()` in Java look similar but serve completely different purposes.
690+
691+
1. **final**It is a **modifier** used for variables, methods, and classes.
692+
- A `final` variable cannot be reassigned once initialized.
693+
- A `final` method cannot be overridden in subclasses.
694+
- A `final` class cannot be extended.
695+
696+
2. **finally** → It is a **block** used in **exception handling** to execute important code such as closing files, releasing connections, etc.
697+
- The `finally` block executes **regardless** of whether an exception occurs or not.
698+
699+
3. **finalize()** → It is a **method** defined in the `Object` class that the **Garbage Collector** calls before destroying an object.
700+
- It is rarely used now because garbage collection timing is unpredictable.
701+
702+
**Example:**
703+
704+
```java
705+
public class FinalExample {
706+
final int VALUE = 100;
707+
708+
public final void display() {
709+
System.out.println("Final method");
710+
}
711+
712+
public static void main(String[] args) {
713+
try {
714+
System.out.println("Inside try block");
715+
} finally {
716+
System.out.println("Inside finally block");
717+
}
718+
}
719+
720+
@Override
721+
protected void finalize() throws Throwable {
722+
System.out.println("Finalize method called");
723+
}
724+
} ```
725+
726+
21. ### What is the difference between '==' and equals() method?
687727

688728
Both **==** and **.equals()** methods are used to compare objects, but they work in different ways:
689729

690-
1. == (Reference comparison)
691-
2. .equals()(Content comparison)
730+
1. == (Reference comparison)
731+
2. .equals()(Content comparison)
732+
733+
**[⬆ Back to Top](#table-of-contents)**
692734

693-
**[⬆ Back to Top](#table-of-contents)**
694735

695736
<!-- QUESTIONS_END -->

0 commit comments

Comments
 (0)