
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is ARM in Java?
Automatic resource management(ARM) or try-with-resources is a new exception handling mechanism that we are going to discuss in this article.
What is a resource?
A resource is an object which implements AutoClosable interface. Whenever you use a resource in your program, it is recommended to close it after use.
For example, if you open a file using FileInputStream, it is recommended to close the stream after usage. This is done to free up the system resources.
In Java, we can use the close() method to close the resources. But if you forget to close the resource, it will lead to memory leaks and other issues.
Initially, this task is done using the finally block.
Example
In the following example, we are using the finally block to close the FileInputStream resource. If you forget to close the stream, it will lead to memory leaks.
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class FinalExample { public static void main(String[] args) throws IOException { File file = null; FileInputStream inputStream = null; try { file = new File("D:\source\sample.txt"); inputStream = new FileInputStream(file); Scanner sc = new Scanner(inputStream); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if(inputStream != null){ inputStream.close(); } } } }
Output
Following is the output of the above program:
This is a sample file with sample text
ARM(Automatic Resource Management) in Java
ARM in Java stands for automatic resource management. It was introduced in Java 7. In it, the resources should be declared in the try block, and they will be closed automatically at the end of the block. It is also known as the try-with-resources block, and the objects we declare in this should be a resource, i.e., they should be of the type AutoClosable.
Syntax
Following is the syntax of the try-with-resources statement:
try (ClassName obj = new ClassName()) { // code... }
From JSE7 onwards, the try-with-resources statement is introduced. In this, we declare one or more resources in the try block, and these will be closed automatically after the use (at the end of the try block).
The resources we declare in the try block should extend the java.lang.AutoCloseable class.
Example
In the following example, we are using the try-with-resources statement to read a file. The FileInputStream class implements the AutoCloseable interface, so we can use it in the try-with-resources statement.
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class FinalExample { public static void main(String[] args) throws IOException { try (FileInputStream inputStream = new FileInputStream(new File("D:\source\sample.txt"))) { Scanner sc = new Scanner(inputStream); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
Output
Following is the output of the above program:
This is a sample file with sample text
Multiple resources in Java
You can also declare multiple resources in try-with-resources, and they all will be closed at once at the end of the block.
Example
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying { public static void main(String[] args) { try (FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt")); FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))) { byte[] buffer = new byte[1024]; int length; while ((length = inS.read(buffer)) > 0) { outS.write(buffer, 0, length); } System.out.println("File copied successfully!!"); } catch (IOException ioe) { ioe.printStackTrace(); } } }
Output
Following is the output of the above program:
File copied successfully!!
Points to remember while using ARM
- To use a class with the try-with-resources statement it should implement the AutoCloseable interface and the close() method of it gets invoked automatically at runtime.
- You can declare more than one class in a try-with-resources statement.
- While you declare multiple classes in the try block of try-with-resources statement these classes are closed in reverse order.
- Except for the declaration of resources within the parenthesis, everything is the same as a normal try/catch block of a try block.
- The resource declared in try gets instantiated just before the start of the try-block.
- The resource declared at the try block is implicitly declared as final.