Given below are the areas in which new features are added and enhancement is done:
Binary literals is a new addition introduced in Java 7, wherein we can express the integral types (byte,short,int & long) by using Binary number System. To specify a binary literal, add the prefix 0b or 0B to the number.
The following example shows how binary literals work :
package com.javabykiran;
public class BinaryLiteLab1 {
public static void main(String[] args) {
int One = 0b001; //1 in binary form
int Two = 0b010; //2 in binary form
System.out.println(One);
System.out.println(Two);
}
}
Output:
1
2
From the time Java 7 was introduced we can write any number of underscore characters like ( _ ) and which can appear anywhere between the digits. This feature enables you to separate groups of digits in numeric literals,which can improve the readability of your code.
It must be kept in mind though, that underscores will be omitted in operations.
Example:
package com.javabykiran;
public class UnderScoreLab1 {
public static void main(String[] args) {
long creditCardNumber = 1234_5678_9012_3456L;
long adharCardNumber = 1232_3400_5432_4564L;
System.out.println(creditCardNumber);
System.out.println(adharCardNumber);
}
}
Output:
1234567890123456
1232340054324564
Before jdk 7, only numbers were allowed in switch Case statement. But now, in jdk 7, we can use string object in switch case Statements.
Switch case strings should be enclosed in "double quotes."
Example:
package com.javabykiran;
public class SwitchCaseLab {
public void getTypeofDay(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
System.out.println(typeOfDay);
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
System.out.println(typeOfDay);
break;
case "Friday":
typeOfDay = "End of work week";
System.out.println(typeOfDay);
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
System.out.println(typeOfDay);
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
}
public static void main(String[] args){
SwitchCaseLab lab = new SwitchCaseLab();
lab.getTypeofDay("Monday");
lab.getTypeofDay("Sunday");
}
}
Output:
Start of work week
Weekend
Question : How to deal with resource management and how has it changed with Java 7?
Managing resources that needed to be explicitly closed was somewhat tedious before Java 7. Any object that implements java.lang.AutoCloseable, can be used as a resource. So, to have the best resource management, you need to:
You have to write finally block to make sure that all the resources are freed before closing of the program before this concept was introduced.
Try with resources that will make sure that the resource will be freed no matter whether try completes abruptly or successfully.
Try must have catch or must have throws clause to use this concept.
Consider the following Example:
package com.javabykiran;
import java.io.FileInputStream;
import java.io.IOException;
public class TryWithResLab {
private static void printFileJava7() throws IOException {
try(FileInputStream input = new FileInputStream ("D:\\notes\\abc.txt")) {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
}
}
public static void main(String[] args) throws IOException {
TryWithResLab.printFileJava7();
}
}
Output:
www.javabykiran.com
Before this concept introduced for handling more than one exception we need to write mutiple catch statements.
Now, in Jdk 7, we can write multiple catch exceptions in single catch statement.
Example: A single catch block can handle more than one type of exception
package com.javabykiran;
import java.sql.SQLException;
public class TryWithMultiLab{
public static void main(String[] args) {
try{
//int i = 10/0;
int i= 10/5;
throw new SQLException();
}catch (ArithmeticException | SQLException e) {
e.printStackTrace();
}
}
}
Output:
java.sql.SQLException
at com. javabykiran. Try With Multi Lab. main (Try With Multi Lab. java:13)