Try With Resources
Try With Resources
(BCS-403)
Prepared By
Sachin Kumar Sonker
Assistant Professor,UCER Naini,Allahabad
The try-with-resources
Try-with-resources statement is a try statement that
declares one or more resources in it.
Java introduced try-with-resource feature in Java 7.
Resource:
A resource is an object that must be closed once
your program is done using it
example, a File resource or a Socket connection
resource.
ensures that each resource is closed at the end of
the statement execution.
Any object can be pass as a resource that implements
java.lang.AutoCloseable
The try-with-resources
don’t need to add an extra finally block for just
passing the closing statements of the resources.
Syntax: Try-with-resources
resources
import java.io.*;
class TWR {
public static void main(String[] args)
{
try (FileOutputStream fos = new
FileOutputStream("outputfile.txt");
BufferedReader br = new
BufferedReader(newFileReader(“firsttextfile.txt"))) {
String text;
while ((text = br.readLine()) != null) {
byte arr[] = text.getBytes();
fos.write(arr);
}
catch (Exception e) {
System.out.println(e);
}
System.out.println( "Resource are closed and
message has been written into the firsttextfile.txt");
}
}
Example
@TestAnnotation(“testing”);
Category 3: Full Annotations
Example
@TestAnnotation(owner=”Rahul”, value=”Class
Geeks”)
Category 4: Type Annotations
Output:
I am annotated with a type annotation
This function's return type is annotated
Category 5: Repeating Annotations
module com.Javatut{
}
.
Leave module body empty, if it does not has
any module dependency. Save this file
inside src/com.javatut with module-
info.java name.
Java Source Code
Now, create a Java file to compile and execute
module. In our example, we have a Hello.java file
that contains the following code.
class Hello{
public static void main(String[] args){
System.out.println("Hello from the Java module");
}
}
Output:
Hello from the Java module
Diamond Operator
What is a diamond operator?
Diamond operator was introduced as a new feature
in java SE 7. The purpose of diamond operator is to
avoid redundant code by leaving the generic type
in the right side of the expression.
main purpose of the diamond operator is to
simplify the use of generics when creating an
object.
avoids unchecked warnings in a program and makes
the program more readable.
Diamond Operator
diamond operator could not be used with
Anonymous inner classes in JDK 7.
In JDK 9, it can be used with the anonymous
class as well to simplify code and improves
readability.
Example:
List<String> c = new ArrayList<>();
Problem with the diamond operator while
working with Anonymous Inner classes
Java 7 allowed us to use diamond operator in normal
classes but it didn’t allow us to use them in
anonymous inner classes. Lets take an example:
$javac JavaExample.java
JavaExample.java:7: error: cannot infer type
arguments for MyClass
MyClass obj = new MyClass<>() {
reason: cannot use '<>' with anonymous inner classe
where T is a type-variable:
T extends Object declared in class MyClass
Java developer extended the feature of the
diamond operator in JDK 9 by allowing the
diamond operator to be used with anonymous
inner classes too.
If we run the above code with JDK 9, then
code will run fine and we will generate the
below output.
Output:
201