Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81a0fa3

Browse files
committed
Java 11 local variable samples
1 parent 093d9a4 commit 81a0fa3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.winterbe.java11;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.function.Predicate;
7+
8+
public class LocalVariableSyntax {
9+
10+
public static void main(String[] args) {
11+
var text = "Banana";
12+
// Incompatible types:
13+
// text = 1;
14+
15+
16+
// Cannot infer type:
17+
// var a;
18+
// var nothing = null;
19+
// var bla = () -> System.out.println("Hallo");
20+
// var method = LocalVariableSyntax::someMethod;
21+
22+
var list1 = new ArrayList<>(); // ArrayList<Object>
23+
24+
var list2 = new ArrayList<Map<String, List<Integer>>>();
25+
26+
for (var current : list2) {
27+
// current is of type: Map<String, List<Integer>>
28+
System.out.println(current);
29+
}
30+
31+
Predicate<String> predicate1 = (@Deprecated var a) -> false;
32+
33+
}
34+
35+
void someMethod() {}
36+
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /