public
Created

Project Coin- Java 7 language enhancements

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
* Created by IntelliJ IDEA.
* User: mohamed
* Date: 1/5/12
* Time: 8:49 PM
* To change this template use File | Settings | File Templates.
*/
public class Java7FeatureDemo {
public static void main(String[] args){
/*
Diamond Operator
*/
//Pre Java 7 style of declaration
Map<String, List<Integer>> myMap = new HashMap<String, List<Integer>>();
 
// Warning issued by compiler
Map<String, List<Integer>> myMap2 = new HashMap();
 
// Java 7 language enhancement - Generic type inference.
Map<String, List<Integer>> myMap3 = new HashMap<>();
 
/*
Strings in Switch statements.
*/
String choice = "A";
 
switch (choice){
case "A":
System.out.println("A chosen");
break;
case "B":
System.out.println("B chosen");
break;
case "C":
System.out.println("C chosen");
break;
}
 
/*
Multi catch blocks
*/
 
try {
exceptionThrowingMethod1();
 
} catch (IOException | NoSuchFieldException e){
e.printStackTrace();
}
 
/*
Binary Integral literals
*/
int binary1 = 0b011;
int binary2 = 0B111;
int hex1 = 0x31;
System.out.println(binary1);
System.out.println(hex1);
System.out.println(binary2);
 
/*
Underscores in Integer literals
*/
// Pre Java 7
int hugeNumber = 1000000000;
// In Java 7
int hugeNumber2 = 1_000_000_000;
System.out.println(hugeNumber == hugeNumber2);
}
 
static void exceptionThrowingMethod1() throws IOException, NoSuchFieldException{}
 
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.