what happens if both try and finally have a return statement?” Sounds simple, right? But this is where many developers get confused. When I first learned this, I thought — 👉 whichever return comes first will be executed. But Java doesn’t work that way. In Java, the finally block always executes, even if a return statement has already been encountered in the try block. And here’s the twist — if finally also contains a return statement, 👉 it completely overrides the return from try. So you might expect the output to be 10… but the actual result will be 20. A small concept, but a big difference in understanding. Also, an important lesson: ❌ Never use return statements inside a finally block It makes your code confusing, hard to debug, and leads to unexpected behavior. The purpose of finally is cleanup — not control flow. Because in programming, it’s not just about writing code… it’s about understanding how it actually works. 🚀 #Java #Programming #SoftwareDevelopment #CodingInterview #Developers #Tech #Learning #CleanCode #JavaConcepts

😊✨️👍🏻yes bhaiya.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories