Grokking Java Interview Questions
0% completed
try with resources
BufferedReader in = new BufferedReader(new StringReader("apple 50")); try (in) { System.out.println(in.readLine()); } in.readLine(); // throws IOException: Stream closed
The code above prints apple 50, and then its last line fails, because the reader is already closed. No line of the code calls close().
The parentheses after try explain both results. The first question covers what the compiler adds to this statement. The others cover the closing order, the exceptions that close() adds, and the way to write a class that can be used here.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%