Skip to content

Instantly share code, notes, and snippets.

@sanaulla123
Created May 1, 2012 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanaulla123/2569878 to your computer and use it in GitHub Desktop.
Save sanaulla123/2569878 to your computer and use it in GitHub Desktop.
Project Coin- Java 7 language enhancements
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{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment