2

Java 7 features has autoclose of resource and multi-catch statement, but how it works internally in both cases?

For example

// Try with resource
public void testTryWithResourcesStatement() throws FileNotFoundException, IOException {
    try (FileInputStream in = new FileInputStream("java7.txt")) {
        System.out.println(in.read());
    }
}

Here there is no need for finally. But how it works internally? How JVM closes it internally?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
shiv
  • 477
  • 5
  • 17
  • why it is nagative voted can any body explain ? what's wrong with it – shiv Jul 22 '17 at 12:10
  • Prior research. Have you tried asking a search engine. And hint: this is what we call syntax sugar. It is not the JVM but the compiler doing the magic. – GhostCat Jul 22 '17 at 12:17
  • This question is pretty broad - it sounds like you're asking someone to explain the implementation details of something deep in the JVM/compiler. – Oliver Charlesworth Jul 22 '17 at 12:18
  • You should take a look at the **bytecode** in general such things will be automatically converted for compatibility and generate the exact same code internally. So *try-with-resources* probably gets converted to the same code than the usual *try-catch-finally and close* stuff. – Zabuzard Jul 22 '17 at 12:21
  • @OliverCharlesworth can you please share any resource related to this. will appreciate. – shiv Jul 22 '17 at 12:22
  • And just another hint : google for AutoClosable. As said: use a search engine. You can do that! Go and try! – GhostCat Jul 22 '17 at 12:22
  • Here is an article explaining what happens under the hood (just a quick google search): https://www.sourceclear.com/blog/Exception-Handling-with-Try-with-Resources-Statement-in-Java-7/ – Zabuzard Jul 22 '17 at 12:23

0 Answers0