Introduction to Kotlin try-with-resources
The following article provides an outline for Kotlin try-with-resources. Kotlin try with resources is used to manage the memory in kotlin, kotlin is managing the memory in very efficient way. As per java developers in kotlin we have no need to manage the memory explicitly. In kotlin, we are using try with resources technique for managing the memory. In kotlin language we have function as use which was taking the burden for managing resources automatically.
The kotlin try with resources is the part of the function of the standard library which was provided by kotlin. As per the documentation of kotlin the use function is defined as an extension of generic on all the types which was closable. In a use function, the block of definition is a function that processes the closeable resources. Basically in kotlin try with resources is a try statement which was used to declare one or more resources. A resource is nothing but an object which was closed once in our program is done by using it. For example, socket connection resource.
Key Takeaways
- Try with resource statement in kotlin will allow us to provide a set of resources that was automatically closed after our code complete its execution.
- We have no need to close files manually. We are using use function in kotlin instead of this function which was used in java.
Why Use Kotlin try-with-resources?
The try with resource statement will ensure that each resource will close at the end of executing the statement. Suppose we are not closing the resources, it will constitute a leak of resource, and our program is also exhausting the resource which was available for it. We are passing an object as a resource, which was implementing the auto closeable object, which includes the object which was implemented by using an object as closeable. By using kotlin try with resource we have no need to add the extra finally block just passing the closing statement for the resources.
At the time of coming exception block, there is a difference between the block of try, catch, and finally and the block of try with resource. Suppose exception is thrown from both try and finally block, then the exception which was thrown from try with the resource is the suppressed exception.
Below example shows why we are using try with resources as follows:
Code:
import java.io.File
fun main (args: Array<String>) {
val trywith = File ("kotlin.txt")
trywith.bufferedReader ().use {
println (it.readText())
}
}
Output:
In the above example, we are using the main function, in the main function we are defining the array string also we are using bufferedReader and use a function with the variable of trywith.
Kotlin try-with-resources Management
We can define the three different phases at the time of working with kotlin try with resources. Below example shows kotlin try with resource management as follows:
Code:
import java.io.File
fun main(args: Array<String>)
{
res = acquireResource()
try
{
useResource(res)
} finally {
releaseResource(res)
}
}
Output:
If the library of language is responsible for the resource releasing, then we call the same as automatic resource management. Such type of feature is relieving us from the remembering the resource which was free. The resource management usually will tied from the scope block. Suppose we are dealing with more than one resource at the same time, they will be released in a good manner.
In the java object which was held the resource and it was eligible for the implementation of resource management which was automatic for a specific interface. Into the kotlin, there is the same concept of the resource holders from which the object will implement either auto closeable or closeable.
How to Use kotlin Resources Function?
For automatically managing the resources many languages have their dedicated construct, sometimes they will be offering a pattern or giving the library method. As per design we can find the method of extension which was used to call use function in standard library.
Below example shows how we can use the function in kotlin resources as follows:
Code:
import java.io.File
fun main(args: Array<String>)
{
val kot_fun = FileWriter ("kotlin.txt")
kot_fun.use
{
kot_fun.write ("kotlin")
}
}
Output:
We are invoking the use function on any object which implements the closeable and auto closeable method by using try with the resource. The use method will take the lambda expression, execute it and it will disposes the resource whenever execution will leaving the block either by using exception of by using normally.
In below example we are using variable called writer by creating the closure. The use function will accept the lambda expression with single parameter as follows.
Code:
import java.io.File
fun main (args: Array<String>) {
FileWriter ("kotlin.txt")
.use { w -> w.write ("kotlin") }
}
Output:
We can also use the implicit variable inside into the block.
Below example shows how to use implicit variable inside into the block as follows.
Code:
import java.io.File
fun main(args: Array<String>) {
FileWriter ("kotlin.txt")
.use { it.write ("kotlin") }
}
Output:
Examples of Kotlin try-with-resources
Below are the examples of kotlin try with resources as follows:
Example #1
In below example we are using variable name as ex to define the example of try with resources in kotlin.
Code:
import java.io.File
fun main(args: Array<String>) {
val ex = File("example.txt")
ex.bufferedReader().use {
println(it.readText())
}
}
Output:
Example #2
The below example shows how we are using use function by using try with resource in kotlin as follows.
Code:
import java.io.File
fun main(args: Array<String>) {
val example = FileWriter ("example.txt")
example.use {
example.write ("Kotlin example")
}
}
Output:
FAQ
Given below is the FAQ mentioned:
Q1. How use function is used in kotlin try with resources?
The below code shows how we are using the use function with kotlin try with resources as follows.
Code:
import java.io.File
val func_use = FileWriter ("use.txt")
func_use.use
{
func_use.write ("Use function")
}
Output:
Q2. What is the syntax of use function in kotlin try with resource?
Syntax:
Inline fun < >, use (block) R (source)
Q3. What is closeable and auto closeable method in kotlin try with resource?
Answer:
We are defining the use function definition with the closeable method in kotlin. In java auto closeable method does not exist.
Conclusion
A resource is nothing but an object which was closed once in our program is done by using it, for example, socket connection resource. Kotlin try with resources is used to manage the memory in kotlin; kotlin is managing the memory in a very efficient way.
Recommended Articles
This is a guide to Kotlin try-with-resources. Here we discuss the introduction, kotlin try-with-resources management, examples and FAQ. You may also have a look at the following articles to learn more –