Java - Try with resources
From Java7, it introduced the mechanism to release the resource automatically as C# does with using statement. In C#, you can use the using statement to release resource allocated by object which derives from IDisposable.
1
2
using (var socket = new Socket(...)) {
}
Java7 can achieve same result with the following syntax. However it cannot use for any object, it can only support the instance which implements java.lang.AutoCloseable as C# does.
1
2
3
4
5
try (
File file = new FileInputStream(...);
File output = new FileInputStream(...);
) {
}