Skip to content

Commit 068a39b

Browse files
committed
adds a couple of experiments with new features of jdk 8, 9 and 10, updates README file
1 parent c19c70d commit 068a39b

File tree

8 files changed

+679
-2
lines changed

8 files changed

+679
-2
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
# java-sandbox
2-
A sandbox repository with code written for learning purpose.
1+
# Java Sandbox
2+
A sandbox-like repository with code written for learning purposes. Includes mainly experiments with main new features of jdk8, jdk9 and jdk10.
3+
4+
## JDK 8 changes
5+
* [Virtual Extension Methods](src/main/java/io/github/ingvarc/jdk8/DefaultMethods.java) ([JEP 126](http://openjdk.java.net/jeps/126))
6+
* [Date & Time API](src/main/java/io/github/ingvarc/jdk8/DateTimeAPI.java) ([JEP 150](http://openjdk.java.net/jeps/150))
7+
* [Stream API](src/main/java/io/github/ingvarc/jdk8/Streams.java)
8+
9+
10+
## JDK 9 changes
11+
* [Convenience Factory Methods for Collections](src/main/java/io/github/ingvarc/jdk9/FactoryMethods.java) ([JEP 269](http://openjdk.java.net/jeps/269))
12+
* [Optional updates](src/main/java/io/github/ingvarc/jdk9/OptionalUpdates.java)
13+
14+
15+
## JDK10 changes
16+
* [Local-variable type inference](src/main/java/io/github/ingvarc/jdk10/LocalVariableTypeInference.java) ([JEP 286](http://openjdk.java.net/jeps/286))

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.github.ingvarc</groupId>
6+
<artifactId>java-sandbox</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>java-sandbox</name>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>10</maven.compiler.source>
13+
<maven.compiler.target>10</maven.compiler.target>
14+
</properties>
15+
16+
</project>
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package io.github.ingvarc.jdk10;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.function.BiFunction;
7+
import java.util.function.Function;
8+
9+
/**
10+
* Experiments with Local-Variable Type Inference.
11+
*/
12+
public class LocalVariableTypeInference {
13+
14+
15+
// Type Inference can only be used for local variables, NOT for class fields
16+
// private var number = 125; // DOESN'T compile
17+
18+
19+
// type inference cannot be used for method parameters
20+
// void greeting(var name) { // DOESN'T compile
21+
// System.out.println("Hello, my name is " + name);
22+
// }
23+
24+
25+
// var is allowed as variable and method name
26+
private static void var() {
27+
var var = "var";
28+
}
29+
30+
31+
// var is not allowed as class name
32+
// private class var{} // DOESN'T compile
33+
34+
35+
// var cannot be used in a method signature
36+
// private var getString() {
37+
// return "foo";
38+
// }
39+
40+
41+
public static void main(String... args) throws IOException {
42+
43+
// simple type inference
44+
var number = 125;
45+
var text = "some text";
46+
var character = 'A';
47+
48+
System.out.println(number);
49+
System.out.println(text);
50+
System.out.println(character);
51+
52+
53+
// multiple variables
54+
int foo = 0, bar = 1;
55+
// var foo = 0, bar = 1; // DOESN'T COMPILE
56+
57+
58+
// `var` is not allowed as an element type of an array
59+
int intArray[] = new int[0];
60+
// var intArray[] = new int[0]; // DOESN'T COMPILE
61+
62+
63+
// `var` is not allowed with array initializer
64+
long[] longArray = {1L, 2L, 3L};
65+
// var longArray = { 1L, 2L, 3L }; // DOESN'T COMPILE
66+
67+
68+
// compiler infers ArrayList<String> instead of List<String>
69+
var strings = new ArrayList<String>();
70+
// so this is a compile error
71+
// strings = new LinkedList<String>(); // DOESN'T COMPILE
72+
73+
74+
// type inference from return type
75+
var name = getName();
76+
var age = getAge();
77+
78+
System.out.println(name);
79+
System.out.println(age);
80+
81+
82+
// type inference from parameterised type
83+
var authors = new ArrayList<Author>();
84+
authors.add(new Author("Walter Isaacson", 65));
85+
authors.add(new Author("Joanne Rowling", 52));
86+
87+
88+
// type inference in loops
89+
for (var author : authors) {
90+
System.out.println(author.getName() + " " + author.getAge());
91+
}
92+
93+
var numbers = List.of("one", "two", "three");
94+
for (String aNumber : numbers) {
95+
System.out.println(aNumber);
96+
}
97+
98+
99+
// type inference in try-with-resources statement
100+
try (var file = new FileInputStream(new File("non-existent-file"))) {
101+
new BufferedReader(new InputStreamReader(file))
102+
.lines()
103+
.forEach(System.out::println);
104+
} catch (IOException ex) {
105+
System.out.println("There's actually no `non-existent-file`");
106+
}
107+
108+
109+
// CharSequence & Comparable comparableCharSequence = getComparableCharSequence("s");
110+
var comparableCharSequence = getComparableCharSequence("The Force will be with you. Always.");
111+
System.out.println(comparableCharSequence.length());
112+
System.out.println(comparableCharSequence.compareTo(""));
113+
114+
115+
// type inference with non-denotable types
116+
Object luke = new Object() {
117+
String name = "Luke Skywalker";
118+
int age = 53;
119+
};
120+
// System.out.println(luke.name + " " + luke.age); // DOESN'T COMPILE
121+
122+
var han = new Object() {
123+
String name = "Han Solo";
124+
int age = 63;
125+
};
126+
System.out.println(han.name + " " + han.age);
127+
128+
129+
// Illegal or impractical use of type inference
130+
131+
// type inference variables must be initialized straight away
132+
// var name; // DOESN'T COMPILE
133+
// name = "Luke"; // DOESN'T COMPILE
134+
135+
// Lambdas must always declare an explicit type
136+
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
137+
// var add = (x, y) -> x + y; // DOESN'T COMPILE
138+
// var nameSupplier = () -> "Luke"; // DOESN'T COMPILE
139+
// var nameFetcher = LocalVariableTypeInference::getName; // DOESN'T COMPILE
140+
141+
String message = "The Force will be with you. Always.";
142+
Function<String, String> quotify = m -> "'" + message + "'";
143+
// var quotify = m -> "'" + message + "'"; // DOESN'T COMPILE
144+
145+
146+
// diamond operator influence
147+
List<Author> fantasyAuthors = new ArrayList<>(); // fantasyAuthors<Author>
148+
var fictionAuthors = new ArrayList<>(); // fictionAuthors<Object>
149+
}
150+
151+
private static <T extends CharSequence & Comparable<T>> T getComparableCharSequence(T text) {
152+
return text;
153+
}
154+
155+
private static String getName() {
156+
return "Luke Skywalker";
157+
}
158+
159+
private static int getAge() {
160+
return 53;
161+
}
162+
163+
private static class Author {
164+
private String name;
165+
private int age;
166+
167+
Author(String name, int age) {
168+
this.name = name;
169+
this.age = age;
170+
}
171+
172+
String getName() {
173+
return name;
174+
}
175+
176+
int getAge() {
177+
return age;
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)