-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
LocalVariableTypeInference.java
71 lines (52 loc) · 2.03 KB
/
LocalVariableTypeInference.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
package java10;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Java 10 finally introduce local variable type inference which allow for local variable to dont have to
* specify the type since is inference already by the signature of the method, class that you consume.
* Not like in Scala here we can only use in local scope, and not in global. And also still not distinctions between
* mutable(var) and immutable(val) variables.
* <p>
* In order to test and c those new capabilities you will need IDE that works with it
* https://www.jetbrains.com/idea/nextversion/
*/
public class LocalVariableTypeInference {
@Test
public void main() {
var str = "hello to inference type world";
System.out.println(str);
var listOfTypes = List.of("is", "cool", "not", "to", "type", "so", "much");
System.out.println(listOfTypes);
var sentence = getHelloWithoutType();
System.out.println(sentence);
var stream = Stream.of("Hello", "Java", "9", "10")
.filter(value -> !value.equals("9"))
.map(String::toUpperCase);
stream.forEach(System.out::println);
var numbers = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.filter(number -> number <= 5)
.map(number -> number * 10)
.boxed()
.collect(Collectors.toUnmodifiableList());
System.out.println(numbers);
var testClass = new TestClass();
var _sentence = testClass.sentence;
System.out.println(_sentence);
var object = new Object() {
String value = "hello";
int number = 0;
};
System.out.println(object.number);
System.out.println(object.value);
}
private String getHelloWithoutType() {
return "Hello without type";
}
private class TestClass {
String sentence = "You looks more like Scala";
}
}