This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Top 8 Java 7 features
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Did you know previous to java 7 you could do a switch on
char
byte
int
Character
Byte
Integer
Java 7 adds String making the switch instruction much friendlier to String inputs
The alternative before was to do with if else statements paired with a bunch of String equal() calls .
The result much cleaner and compact code.
Here the java 7 switch with string
One of the java 7 new feature strings in switch
Example program on java 7 new feature string in switch:
package com.instanceofjavaforus;
public class Demo{
public static void main(String args[]){
String color="green";
switch(color){
case "white": System.out.println("white");
break;
case "green": System.out.println("green");
break;
case "red": System.out.println("red");
break;
case "yellow": System.out.println("yellow");
break;
case "blue": System.out.println("blue");
break;
case "pink": System.out.println("pink");
break;
case "violet": System.out.println("violet");
break;
default: System.out.println("invalid color")
}
}
}
Click for Output
green
2.Type inference for generic instance creation:
Previously when we are using generics you had to specify the type twice, in the declaration and the constructor
Before java 7 to create arraylist of string type
List<String> str= new ArrayList<String>();
In java 7 you just use the diamond <> operator without the type.
List<String> str= new ArrayList<>();
package com.instanceofjava;
public class Java7feture{
public static void main(String args[]){
List<String> lst= new ArrayList<>();
lst.add("ABC");
lst.add("XYZ");
}
}
3.The try with resources Statement:
The new try statement allows opening up a "resource" in atry block and automatically closing the resource when the block is done.
For example in the below piece of code we opened a file print line by line to stdout.
But pay close attention to the finally block.
package com.instanceofjava;
public class Java7feture{
public static void main(String args[]){
BufferedReader in=null;
try{
in= new BufferedReader(new filereader("test.txt"));
String line=null;
while((line=in.readLine())!=null)){
System.out.println(line);
}
}catch(IOException e){
e.printStackTrace();
}
finally{
try{
if(in!=null)
in.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
when using a resource that has to be closed , a finally block is needed to make the clean up code is executed eve if there are exceptions thrown back.
The new try catch with resources allows us to automatically close these resources in a more compact set of code.
package com.instanceofjava;
public class Java7feture{
public static void main(String args[]){
try(
BufferedReader in= new BufferedReader(new filereader("test.txt"))
)
{
String line=null;
while((line=in.readLine())!=null)){
System.out.println(line);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
4.Multiple exception handling System:
Tired of repetitive error handling code ?
APIs like java.io and java.lang.reflect?
package com.instanceofjava;
public class Demo{
public static void main(String args[]){
try{
Class a= Class.forName("wrongclassname");
Object instance=a.newInstance();
}
catch( ClassNotFoundException e){
System.out.println("Failed to create instance") ;
}
catch( IllegalAccessException e){
System.out.println("Failed to create instance") ;
}
catch( InstanctiationException e){
System.out.println("Failed to create instance") ;
}
}
}
When the exception handling is basically the same, the improved catch operator now supports multiple exceptions in a single statement separated by "|".
package com.instanceofjava;
public class Java7feture{
public static void main(String args[]){
try{
Class a= Class.forName("wrongclassname");
Object instance=a.newInstance();
}
catch( ClassNotFoundException | IllegalAccessException | InstanctiationException ex ){
System.out.println("Failed to create instance") ;
}
}
}
5.Bracket notation for collections:
package com.instanceofjava;
public class Demo5{
public static void main(String args[]){
List<String> lststr = new ArrayList<String>();
lststr .add("instanceof");
lststr .add("java!");
}
}
package com.instanceofjava;
public class Java7FeatureTutorial{
public static void main(String args[]){
List<Integer> intlst = [1, 2, 3, 4];
Set<String> str = {"abc", "xyz", "dcv"};
Map<String, Integer> map= { "number" : 42 };
}
}
6.Underscored in Numeric literals:
package com.instanceofjava;
public class Java7FeatureTutorial{
public static void main(String args[]){
int value = 1_000_000;
long Atmcardnumber = 0123_4567_8901_2345L; //16 digit number
long ssn = 777_99_8888L;
double pi = 3.1415_9265;
float pifloat = 3.14_15_92_65f;
}
}
7.Binary Literals with Prefix 0b:
package com.instanceofjava;
public class Java7FeatureTutorial{
public static void main(String args[]){
// An 8-bit 'byte' value:
byte byte = 0b00100001;
// A 16-bit 'short' value:
short short = 0b0010001010001010;
// Some 32-bit 'int' values:
int a = 0b10100001010001011010000101000101;
int b= 0b101;
int c = 0B101; // The B can be upper or lower case.
}
}
8.java.nio.file package:
java 7 introduced java.nio and sub package java
java.nio.file
java.nio.file.attribute
Support for input output file. and to access default file system.