The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 8 Java 7 features

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 8 Java 7 features Posted: Jan 30, 2015 6:20 AM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
java 7 features with examples

  1. String in switch statements
  2. Type inference for generic instance creation.
  3. The try with resources Statement.
  4. Multiple exception handling System.
  5. Bracket notation for collections.
  6. Underscored in Numeric literals
  7. Binary Literals with Prefix 0b
  8. 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. }

Click for Output

 

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.  BufferedReader in=null;
  7. try{
  8. in= new BufferedReader(new filereader("test.txt"));
  9. String line=null;
  10. while((line=in.readLine())!=null)){
  11. System.out.println(line);
  12. }
  13. }catch(IOException e){
  14.  e.printStackTrace();
  15. }
  16. finally{
  17. try{
  18. if(in!=null)
  19. in.close();
  20.  }
  21. catch(IOException e){
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

  • 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.Bracket notation for collections:

  1. package com.instanceofjava;
  2.  
  3. public class Demo5{
  4.  
  5. public static void main(String args[]){
  6.  
  7. List<String> lststr = new ArrayList<String>();
  8. lststr .add("instanceof");
  9. lststr .add("java!");

  10. }
  11. }

  

  1. package com.instanceofjava;
  2.  
  3. public class Java7FeatureTutorial{
  4.  
  5. public static void main(String args[]){
  6.  
  7. List<Integer> intlst = [1, 2, 3, 4];
  8.  
  9. Set<String> str = {"abc", "xyz", "dcv"};
  10.  
  11. Map<String, Integer> map= { "number" : 42 };

  12. }
  13. }

 

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

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

 

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.  

Read: Top 8 Java 7 features

Topic: PrimeFaces: Opening external pages in dynamically generated dialog Previous Topic   Next Topic Topic: Testing System.in and System.out with system-rules

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use