Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit 0d0042b

Browse files
committed
added some miscellaneous example
1 parent 67ca2f5 commit 0d0042b

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.hemantsonu20.java9.miscellaneous;
2+
3+
import java.util.concurrent.Callable;
4+
5+
public class DiamondOperator {
6+
7+
public static void main(String[] args) throws Exception {
8+
9+
java8();
10+
java9();
11+
}
12+
13+
private static void java8() throws Exception {
14+
15+
Callable<String> c = new Callable<String>() {
16+
17+
@Override
18+
public String call() throws Exception {
19+
return "call";
20+
}
21+
};
22+
c.call();
23+
}
24+
25+
/**
26+
* Java 8 threw this error with this code
27+
*
28+
* <pre>
29+
*
30+
* Callable<String> c = new Callable<>() {
31+
* ^
32+
* reason: cannot use '<>' with anonymous inner classes
33+
* where V is a type-variable:
34+
* V extends Object declared in interface Callable
35+
*
36+
* </pre>
37+
*
38+
* @throws Exception
39+
*/
40+
private static void java9() throws Exception {
41+
42+
Callable<String> c = new Callable<>() {
43+
44+
@Override
45+
public String call() throws Exception {
46+
return "call";
47+
}
48+
};
49+
c.call();
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.hemantsonu20.java9.miscellaneous;
2+
3+
public interface PrivateMethodsInInterface {
4+
5+
// java 7
6+
void method1();
7+
8+
// java 8, default methods
9+
default void method2() {
10+
// default implementation if not provided in implementing classes
11+
System.out.println("method2");
12+
}
13+
14+
// java 8, static methods
15+
static void method3() {
16+
System.out.println("method3");
17+
}
18+
19+
// java 9, private static methods
20+
private static void method4() {
21+
System.out.println("method4");
22+
23+
}
24+
25+
default void method4Call() {
26+
method4();
27+
}
28+
29+
// java 9, private non-static methods
30+
private void method5() {
31+
System.out.println("method5");
32+
}
33+
34+
default void method5Call() {
35+
method5();
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.hemantsonu20.java9.miscellaneous;
2+
3+
public class TryWithResources {
4+
5+
public static void main(String[] args) throws Exception {
6+
7+
java8();
8+
java9();
9+
}
10+
11+
private static void java8() throws Exception {
12+
13+
try (MyCloseable m1 = new MyCloseable(); MyCloseable m2 = new MyCloseable()) {
14+
// try block
15+
}
16+
}
17+
18+
private static void java9() throws Exception {
19+
20+
// object can be instantiated outside try, even in this case, close will be
21+
// called automatically
22+
MyCloseable m1 = new MyCloseable();
23+
MyCloseable m2 = new MyCloseable();
24+
try (m1; m2) {
25+
// try block
26+
}
27+
}
28+
29+
public static class MyCloseable implements AutoCloseable {
30+
31+
@Override
32+
public void close() throws Exception {
33+
34+
System.out.println("close called");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)