0% found this document useful (0 votes)
37 views

Javase 7 Features: String in Switch Statement

JavaSE 7 introduced several new features including string literals in switch statements, binary and underscore literals in numeric literals, try-with-resources statement to automatically close resources, catching multiple exception types in a single catch block, and the G1 garbage collector. It also included type inference with the diamond operator and safe varargs annotation.

Uploaded by

shaik baresha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Javase 7 Features: String in Switch Statement

JavaSE 7 introduced several new features including string literals in switch statements, binary and underscore literals in numeric literals, try-with-resources statement to automatically close resources, catching multiple exception types in a single catch block, and the G1 garbage collector. It also included type inference with the diamond operator and safe varargs annotation.

Uploaded by

shaik baresha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaSE 7 Features

1. String in switch statement


2. Binary Literals
3. The try-with-resources
4. Caching Multiple Exceptions by single catch
5. Underscores in Numeric Literals
6. SafeVarargs
7. G1 Garbage Collector

String in switch statement:

Switch statements work either with primitive types or enumerated types. Java 7
introduced another type that we can use in Switch statements: the String type.

public void testStringInSwitch(String param){


final String JAVA5 = "Java 5";
final String JAVA6 = "Java 6";
final String JAVA7 = "Java 7";
switch (param) {
case JAVA5:
System.out.println(JAVA5);
break;
case JAVA6:
System.out.println(JAVA6);
break;
case JAVA7:
System.out.println(JAVA7);
break;
}
}

Underscores in Numeric Literals:

Java 7 introduced underscores in identifying the places.

int one_million = 1_000_000;


Binary Literals:

In JDK 7, you can express literal values in binary with prefix '0b' (or '0B') for integral
types (byte, short, int and long).

Eg: int mask = 0b01010000101;

or

int binary = 0B0101_0000_1010_0010_1101_0000_1010_0010;

try-with-resources:

Java 7 has introduced another cool feature to manage the resources automatically.

public void testTryWithResourcesStatement() throws FileNotFoundException, IO


Exception{
try (FileInputStream in = new FileInputStream("java7.txt")) {
System.out.println(in.read());
}
}

Caching Multiple Exceptions by single catch:

In JDK 7, a single catch block can handle more than one exception types.

try { ...... } catch(ClassNotFoundException|SQLException ex) { ex.printStackTrace(); }

SafeVarargs:

Diamond operator <> is a new Java 7 feature which provides type inference while
creating object of Generic classes.

public void testDinamond(){


List list = new ArrayList<>();
Map> map = new HashMap<>();
}
Map<String, List<Trade>> trades = new TreeMap <> ();
G1 Garbage Collector: DK 7 introduced a new Garbage Collector known as G1 Garbage
Collection, which is short form of garbage first. G1 garbage collector performs clean-up
where there is most garbage.

You might also like