Open In App

use Keyword in Kotlin with Example

Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. In this article, we are going to discuss the use of use keyword in kotlin. before that I want you to know some basics for the better understanding.

Basic Idea

There are some situations where if you use a resource (for example, a file) then you have to take care of its lifecycle so that you don’t leak resources. For example, if you read from a file, you need to close it after use, or else you’ll leave it in an unstable state. Java 7 brought an update that could handle this without a need to handle it explicitly. Kotlin also provides this feature but in a much easier way. It does so by using the use method. So let’s get started!

Example

Let’s take the following steps to understand the use function of Kotlin:

Step 1. To understand the use keyword, we will need to go back to Java. Prior to Java 7, managing the resources that needed to be closed was a bit cumbersome. For example, look at the following code:

Kotlin




private static void printFile() throws IOException {
  InputStream input = null;
  try {
    input = new FileInputStream ("sampleFile.txt");
    // Some operation using input object
  } finally {
    if(input != null){
      input.close();
      // closing the resource
    }
  }
}


Step 2. Let’s examine the preceding code. We know an exception can be thrown inside the try block when we use the input object. However, it can also be thrown in the finally block, because we are trying to close the input object. Now the finally block will be called whether or not the try block throws an exception. Suppose both the try and finally blocks throw exceptions-which one of the two will propagate? The answer is that the exception will be thrown in the finally block, even if the exception of try would make more sense here.

Step 3. Java 7 brought an update to this problem by introducing the try-with-resource construct, which looks something like this:

Kotlin




try (FileInputStream input = new FileInputStream ("file. txt")) {
  int data-input.read();
  // operations on input object
}


Step 4. When the try block finishes executing, the FileInputStream object is closed automatically. Also, if both the operations-input.read() and the closing of the input object-throw exceptions, the exception thrown by input.read() will propagate. The use keyword of Kotlin does the exact same work. In this section, we will see how.

Step 5. In the preceding example, we saw in Java will look something like the following in Kotlin if we implement the use keyword:

Kotlin




FileInputStream ("file. txt") . use {
  input ->
  var data = input.read()
}


use accepts a function literal and is defined as an extension on an instance of closeable. It will close down the resource, just like the try-with-resources construct, after the function has been completed, whether an exception was raised or not.



Last Updated : 03 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads