JavaSE 7 Features
The important features of JavaSE 7 are try with resource, catching multiple exceptions etc.- String in switch statement (Java 7)
- Binary Literals (Java 7)
- The try-with-resources (Java 7)
- Caching Multiple Exceptions by single catch (Java 7)
- Underscores in Numeric Literals (Java 7)
Try-with-resources Statement
- The
try
-with-resources
statement is a try
statement that declares one or more resources.
- A resource
is an object that must be closed after the program is finished with it.
- The
try
-with-resources statement
ensures that each resource is closed at the end of the statement.
- Any object that implements
java.lang.AutoCloseable
, which includes all objects which implement java.io.Closeable
, can be used as a resource.
- The
try
-with-resources statement is atry
statement that declares one or more resources. - A resource
is an object that must be closed after the program is finished with it.
- The
try
-with-resources statement ensures that each resource is closed at the end of the statement. - Any object that implements
java.lang.AutoCloseable
, which includes all objects which implementjava.io.Closeable
, can be used as a resource.
The following example reads the first line from a file. It
uses an instance of
BufferedReader
to read data from the file. BufferedReader
is a
resource that must be closed after the program is finished with it:static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
Q):- What Type of resource we can use with “try-with-resource” statements :-
Ans:- Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
JavaSE 8 Features
1):- Parallel Sort [JavaSE 8]
public class Demo
{
public static void
main(String args[])
{
String
fruits[] = {"guava","apple","banana"};
String
fruits1[] = {"guava","apple","banana"};
//
before JDK 1.8, using Arrays.sort()
Arrays.sort(fruits);
System.out.println(Arrays.toString(fruits));
// with JDK 1.8, using Arrays.parallelSort()
Arrays.parallelSort(fruits1);
System.out.println(Arrays.toString(fruits1));
}
}
2):- Addition of Calendar.Builder [JavaSE 8]
Before JDK 1.8, each date field is set
separately with individual methods. Each set method added as a separate
statement
import
java.util.Calendar;
// for Calendar class
import static
java.util.Calendar.*; // for
static methods of Calendar class
public class Demo
{
public static void
main(String args[])
{
Calendar
calendar = Calendar.getInstance();
// populate calendar with individual
set methods one-by-one as a separate statement
calendar.set(YEAR,
2013);
calendar.set(MONTH,
APRIL);
calendar.set(DATE,
10);
calendar.set(HOUR,
8);
calendar.set(MINUTE,
56);
calendar.set(SECOND,
14);
calendar.set(AM_PM,
PM);
System.out.println(calendar.getTime());
// let us see the JFK 1.8 style
// all set methods are set as one statement
Calendar
calendar1 = new Calendar.Builder()
.set(YEAR, 2013)
.set(MONTH, APRIL)
.set(DATE, 10)
.set(HOUR, 8)
.set(MINUTE, 56)
.set(SECOND, 14)
.set(AM_PM, PM)
.build(); // one semicolon here
System.out.println(calendar1.getTime());
}
}
3):- Introduction of Functional Interfaces [JavaSE 8]
A new annotation, @FunctionalInterface, is introduced to raise compilation error if an interface marked as @FunctionalInterface contains more than one abstract method.
public interface Demo
{
public abstract void
display();
public abstract void
show();
}
4):- Lambda Introduction [JavaSE 8]
"-> " is known
as lambda expression.
import java.util.*;
public class Demo
{
public static void
main(String args[])
{
List<String>
alphabets = Arrays.asList("A", "B", "C",
"D");
System.out.println("Printing
with earlier JDK 1.5 for loop:");
for(String
str : alphabets)
{
System.out.print(str
+ "\t");
}
System.out.println("\nPrinting
with latest forEach loop introduced with JDK 1.8 with lambda usage:");
alphabets.forEach(str ->
{
System.out.print(str
+ "\t");
}
);
}
}
Ex: cities.forEach((String str) -> System.out.print(str + “\t”));
5) default and
static methods in Interfaces :
From
Java8 We can add two type of method with implementation in interface
a) default method
b) static method
@FunctionalInterface
interface InterfaceWithDefaultAndStaticMethod {
void printDemo(String name);
default void defaultMethod() {
System.out.println("InterfaceWithDefaultAndStaticMethod : defaultMethod");
}
static void staticMethod() {
System.out.println("InterfaceWithDefaultAndStaticMethod : staticMethod");
}
}
@FunctionalInterface
interface InterfaceWithDefaultAndStaticMethod1 {
void printDemo(String name);
default void
defaultMethod() {
System.out.println("InterfaceWithDefaultAndStaticMethod1 : defaultMethod");
}
static void staticMethod() {
System.out.println("InterfaceWithDefaultAndStaticMethod1 : staticMethod");
}
}
package com.akshay.lambda;
public class DefaultAndStaticInterfaceDemo
implements InterfaceWithDefaultAndStaticMethod,
InterfaceWithDefaultAndStaticMethod1 {
@Override
public void defaultMethod() {
// TODO
Auto-generated method stub
InterfaceWithDefaultAndStaticMethod.super.defaultMethod();
InterfaceWithDefaultAndStaticMethod1.super.defaultMethod();
}
public void printDemo(String name) {
System.out.println(name);
}
public DefaultAndStaticInterfaceDemo() {
super();
}
public static void main(String args[]) {
DefaultAndStaticInterfaceDemo
obj = new
DefaultAndStaticInterfaceDemo();
obj.printDemo("Akshay");
obj.defaultMethod();
InterfaceWithDefaultAndStaticMethod.staticMethod();
InterfaceWithDefaultAndStaticMethod1.staticMethod();
}
}
Output:-
Akshay
InterfaceWithDefaultAndStaticMethod : defaultMethod
InterfaceWithDefaultAndStaticMethod1 :
defaultMethod
InterfaceWithDefaultAndStaticMethod : staticMethod
InterfaceWithDefaultAndStaticMethod1 : staticMethod
If we implement two interface in which we have
same default method then we need to override in implementation class otherwise
it will throw compile time error.
6):- Replacement of PermGen with Metaspace [JavaSE 8]
JDK 1.8 officially announced the
removal of Permanent Generation (PermGen) space and in its place
introduced Metaspace. In HotSpot VM, the PermGen used to give OutOfMemoryError
due to depletion of space, may sometimes be caused by memory leaks while
loading and unloading a J2EE applications.
No comments:
Post a Comment