Skip to content

Instantly share code, notes, and snippets.

@arjunrao87
Last active October 10, 2018 23:51
Show Gist options
  • Save arjunrao87/77aaa1de6d7a4d8bf0c6e81d5fce3752 to your computer and use it in GitHub Desktop.
Save arjunrao87/77aaa1de6d7a4d8bf0c6e81d5fce3752 to your computer and use it in GitHub Desktop.
SNIPPET 1
// Before Java 7
List<Integer> myList = new ArrayList<Integer>();
// After Java 7
List<Integer> myList = new ArrayList<>();
// Before Java 9
return new Callable<String>() {
@Override
public String call() throws Exception {
return null;
}
};
// After Java 9
return new Callable<>() {
@Override
public String call() throws Exception {
return null;
}
};
SNIPPET 2
// Before Java 7
BufferedReader reader1 = null;
try {
reader1 = new BufferedReader(new FileReader("myFile.txt"));
System.out.println(reader2.readLine());
} catch( IOException e ){
e.printStackTrace();
} finally{
try{
if( reader1 != null ){
reader1.close()
}
} catch( IOException e ){
e.printStackTrace();
}
}
// After Java 7
BufferedReader reader1 = new BufferedReader(new FileReader("myFile.txt"));
try (BufferedReader reader2 = reader1) {
System.out.println(reader2.readLine());
}
// After Java 9
BufferedReader reader1 = new BufferedReader(new FileReader("myFile.txt"));
try (reader1) {
System.out.println(reader1.readLine());
}
SNIPPET 3
// Java 7
interface Java7Interface{
int sum=0; // public static final int sum=0; constants
void fun(); // public abstract void fun(); abstract method
}
// Java 8
interface Java8Interface{
int sum=0; //constants
void method1(); //abstract method
default void method2(){
//implementation in interface itself
}
default static void method3(){
// Static method implemented in interface
}
}
// Java 9
interface Java9Interface{
int sum=0; //constants
void method1(); //abstract method
default void method2(){
//implementation in interface itself
}
default static void method3(){
// Static method implemented in interface
}
private default void privateMethod1(){
// This method is not accessible in its implementer
}
private static default void privateMethod2(){
// This method is not accessible in its implementer
}
}
// Snippet 4
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment