java 7 features with examples
Java 7 features


  1. String in switch statements
  2. Type inference for generic instance creation.
  3. The try with resources Statement.
  4. Multiple exception handling System.
  5. Underscored in Numeric literals
  6. Binary Literals with Prefix 0b
  7. java.nio.file package






1.String in switch statements:

  • Did you know previous to java 7 you could do a switch on
    1. char
    2. byte
    3. int
    4. Character
    5. Byte
    6. 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:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  String color="green";
  8.  
  9. switch(color){
  10.  
  11.    case "white": System.out.println("white");
  12.       break;
  13.  
  14.    case "green": System.out.println("green");  
  15.       break;
  16.  
  17.    case "red": System.out.println("red"); 
  18.      break; 
  19.  
  20.    case "yellow": System.out.println("yellow"); 
  21.      break; 
  22.  
  23.    case "blue": System.out.println("blue");    
  24.        break;  
  25.  
  26.    case "pink": System.out.println("pink");
  27.       break; 
  28.  
  29.    case "violet": System.out.println("violet"); 
  30.        break;  
  31.  
  32.      
  33.    default: System.out.println("invalid color")
  34. }
  35. }



 

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<>();

  1. package com.instanceofjava;
  2.  
  3. public class Java7feture{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  List<String> lst= new ArrayList<>();
  8.   lst.add("ABC");
  9.   lst.add("XYZ");

  10. }
  11. }

 

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.



  1. package com.instanceofjava;
  2.  
  3. public class Java7feture{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  BufferedReader in=null;
  8. try{
  9. in= new BufferedReader(new filereader("test.txt"));
  10. String line=null;
  11. while((line=in.readLine())!=null)){
  12. System.out.println(line);
  13. }
  14. }catch(IOException e){
  15.  e.printStackTrace();
  16. }
  17. finally{
  18. try{
  19. if(in!=null)
  20. in.close();
  21.  }
  22. catch(IOException e){
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }



  • 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.

  1. package com.instanceofjava;
  2.  
  3. public class Java7feture{
  4.  
  5. public static void main(String args[]){
  6.  
  7. try(
  8. BufferedReader  in= new BufferedReader(new filereader("test.txt"))
  9. )
  10. {
  11.  
  12. String line=null;
  13. while((line=in.readLine())!=null)){
  14. System.out.println(line);
  15. }

  16. }catch(IOException e){
  17.  e.printStackTrace();
  18. }
  19. }
  20. }

 

4.Multiple exception handling System:

  • Tired of repetitive error handling code ?
  • APIs like java.io and java.lang.reflect?


  1. package com.instanceofjava;
  2.  
  3. public class Demo{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  try{
  8. Class a= Class.forName("wrongclassname");
  9. Object instance=a.newInstance();

  10. }
  11. catch( ClassNotFoundException e){
  12. System.out.println("Failed to create instance") ;
  13. }
  14. catch( IllegalAccessException e){
  15. System.out.println("Failed to create instance") ;
  16. }
  17. catch( InstanctiationException e){
  18. System.out.println("Failed to create instance") ;
  19. }
  20.  

  21. }
  22. }

  • When the exception handling is basically the same, the improved catch operator now supports
    multiple exceptions in a single statement separated by "|".

  1. package com.instanceofjava;
  2.  
  3. public class Java7feture{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  try{
  8. Class a= Class.forName("wrongclassname");
  9. Object instance=a.newInstance();

  10. }
  11. catch( ClassNotFoundException  | IllegalAccessException  | InstanctiationException ex ){
  12. System.out.println("Failed to create instance") ;
  13. }
  14.  

  15. }
  16. }

  

5.Underscored in Numeric literals:


  1. package com.instanceofjava;
  2.  
  3. public class Java7FeatureTutorial{
  4.  
  5. public static void main(String args[]){
  6.  
  7. int value = 1_000_000;  
  8.  
  9. long Atmcardnumber =  0123_4567_8901_2345L; //16 digit number
  10.  
  11. long ssn = 777_99_8888L;
  12.  
  13. double pi = 3.1415_9265;
  14.  
  15. float  pifloat = 3.14_15_92_65f;

  16. }
  17. }

6.Binary Literals with Prefix 0b:



  1. package com.instanceofjava;
  2.  
  3. public class Java7FeatureTutorial{
  4.  
  5. public static void main(String args[]){
  6.  
  7. // An 8-bit 'byte' value:
  8. byte byte = 0b00100001;
  9.  
  10. // A 16-bit 'short' value:
  11. short short = 0b0010001010001010;
  12.  
  13. // Some 32-bit 'int' values: 
  14. int a = 0b10100001010001011010000101000101;
  15.  
  16. int b= 0b101;
  17.  
  18. int c = 0B101; // The B can be upper or lower case.

  19. }
  20. }

 

7.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.  

You might like:

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Top 7 Java 7 features

  1. 5.Bracket notation for collections: this is not working.

    ReplyDelete

Select Menu