Skip to content

Commit d756749

Browse files
committed
added example Java 18
1 parent 94c767f commit d756749

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A project to explore more about the new features of Java 8, 9, ...
1313
* Pattern matching for `switch` (preview 2)
1414
* Deprecated method `finalize`
1515

16-
* [Java 17](java-17/)
16+
* [Java 17](java-17/) (Sep, 2021)
1717
* Sealed classes (standard)
1818
* Enhanced Pseudo-Random Number Generator
1919
* Deprecate the Applet API for Removal
@@ -40,7 +40,7 @@ A project to explore more about the new features of Java 8, 9, ...
4040
* Foreign-Memory Access API (incubator)
4141
* GCs ZGC and Shenandoah turned final
4242

43-
* [Java 14](java-14/) (May, 2020)
43+
* [Java 14](java-14/) (Mar, 2020)
4444
* Switch expression (standard)
4545
* Pattern matching for `instanceof` (preview)
4646
* Text blocks improvements (preview 2)
@@ -49,7 +49,7 @@ A project to explore more about the new features of Java 8, 9, ...
4949
* Packaging tool
5050
* JFR even streaming
5151

52-
* [Java 13](java-13/)
52+
* [Java 13](java-13/) (Sep, 2019)
5353
* Switch expression (preview 2)
5454
* Text blocks (preview)
5555
* String API updates
@@ -58,28 +58,28 @@ A project to explore more about the new features of Java 8, 9, ...
5858
* Dynamic CDS Archives
5959
* ZGC: Uncommit Unused Memory
6060

61-
* [Java 12](java-12/)
61+
* [Java 12](java-12/) (Mar, 2019)
6262
* Switch expression (preview)
6363
* Compact Number Format
6464
* Collectors improvements
6565
* CompletableFuture improvements
6666
* CDS enable by default
6767
* New GC and improvements
6868

69-
* [Java 11](java-11/)
69+
* [Java 11](java-11/) (Sep, 2018)
7070
* Removal of Java EE Modules, JavaFX and deprecated API
7171
* Http Client (incubator)
7272
* var in lambda expressions
7373
* API improvements
7474
* Null I/O
7575

76-
* [Java 10](java-10/) (March, 2018)
76+
* [Java 10](java-10/) (Mar, 2018)
7777
* Process API improvements
7878
* Collections improvements
7979
* Application Class-Data Sharing
8080
* `var` keyword to declare variables
8181

82-
* [Java 9](java-9/)
82+
* [Java 9](java-9/) (Sep, 2017)
8383
* Milling Project Coin
8484
* Process API
8585
* Platform Logging API and Service
@@ -90,7 +90,7 @@ A project to explore more about the new features of Java 8, 9, ...
9090
* JShell
9191
* much more (see JEPs)
9292

93-
* [Java 8](java-8/)
93+
* [Java 8](java-8/) (Marc, 2014)
9494
* API
9595
* Stream API
9696
* Project Lambda
@@ -102,4 +102,4 @@ A project to explore more about the new features of Java 8, 9, ...
102102
* [Inside.java - Sip of Java](https://inside.java/2021/10/21/sip24/)
103103
* [Considerations Bumping Java EE](https://vorozco.com/blog/2020/2020-08-21-considerations-bumping-javaee.html)
104104
* [The Role of Preview Features in Java and Beyond](https://blogs.oracle.com/javamagazine/the-role-of-previews-in-java-14-java-15-java-16-and-beyond)
105-
* [Place to get early releases from Oracle's JDK](https://jdk.java.net/)
105+
* [Place to get early releases from Oracle's JDK](https://jdk.java.net/)

java-18/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ To run each example use: `java --enable-preview --source 18 <FileName.java>`
66

77
### Language
88

9+
* chartset UTF-8 will be default for all O.S.
10+
* to see which is default: `java -XshowSettings:properties -version 2>&1 | grep file.encoding`
11+
* we can change the chartset with property: `-Dfile.encoding=UTF-8`
12+
* affected classes:
13+
* Stream Reader and Writer
14+
* File Reader and Writer
15+
* `Formatter` and `Scanner`
16+
* `URLEncoder` and `URLDecoder`
17+
* the `System.out` and `System.err` will use the same charset from the terminal
18+
* we can see with: `Console.charset()`
919
* Pattern matching for `switch`
1020
* Minor improvements from JDK 17:
1121
* refined to use dominance checking that will force constant case label to appear before a guarded pattern of the same type;

java-18/SimpleWebServer.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import com.sun.net.httpserver.*;
2+
import java.net.*;
3+
import java.io.*;
4+
import java.nio.file.*;
5+
import java.util.function.Predicate;
6+
7+
/**
8+
* To run from the terminal we can use the utility command-line tool:
9+
* `jwebserver -b 0.0.0.0 -p 8080`
10+
*
11+
* The API only supports GET and HEAD requests, but we can added manual support with `HttpHandlers.handleOrElse`.
12+
*
13+
* Doc: https://download.java.net/java/early_access/jdk18/docs/api/jdk.httpserver/com/sun/net/httpserver/SimpleFileServer.html
14+
*/
15+
public class SimpleWebServer {
16+
private static final String SERVER_DIR = System.getProperty("user.dir");
17+
18+
public static void main(String[] args) {
19+
var server = SimpleFileServer.createFileServer(
20+
new InetSocketAddress(8080),
21+
Path.of(SERVER_DIR),
22+
SimpleFileServer.OutputLevel.VERBOSE
23+
);
24+
25+
var defaultHandler = HttpHandlers.of(404, new Headers(), "Not found");
26+
27+
Predicate<Request> IS_POST = req -> "POST".equals(req.getRequestMethod());
28+
29+
var postHandler = HttpHandlers.handleOrElse(
30+
IS_POST,
31+
(exchange) -> {
32+
System.out.println("Handling POST request");
33+
34+
System.out.println("Headers:");
35+
exchange.getRequestHeaders().forEach((k, v) -> System.out.printf("\t%s: %s\n", k, v));
36+
37+
InputStream requestBody = exchange.getRequestBody();
38+
try (var reader = new BufferedReader(new InputStreamReader(requestBody))) {
39+
System.out.println("\nBody:");
40+
reader.lines().forEach(line -> System.out.printf("\t%s", line));
41+
} catch (RuntimeException | IOException ex) {
42+
System.err.println("Error reading request body: " + ex.getMessage());
43+
}
44+
45+
var responseBody = "POST handled successfully".getBytes();
46+
exchange.sendResponseHeaders(200, responseBody.length);
47+
exchange.getResponseBody().write(responseBody);
48+
},
49+
defaultHandler
50+
);
51+
server.createContext("/post", postHandler);
52+
53+
server.start();
54+
System.out.println("Server started at " + server.getAddress());
55+
}
56+
}

java-18/SwitchWithPatternMatchingSecondPreview.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ public static void main(String[] args) {
1313
System.out.println(stringify(-42));
1414
System.out.println(stringify("Some text"));
1515
System.out.println(stringify(""));
16+
System.out.println(stringify(null));
1617
}
1718

1819
static String stringify(Object value) {
1920
return switch (value) {
2021
// the constant must be before the guarded pattern (otherwise it will never hit)
21-
case 42 -> "42 is the answer";
22+
case Integer i && i == 42 -> "42 is the answer";
23+
case Integer i && i > 0 -> "positive number";
2224
case Integer i && i < 0 -> "negative number";
2325
// this must be after because it will match all integers
24-
case Integer i -> "some number";
26+
case Integer i -> "should be 0";
2527

2628
case String s && s.isEmpty() -> "empty string";
2729
case String s && s.length() > 50 -> "long string";
2830
// this must be after because it will match all strings
2931
case String s -> "non-empty string";
32+
// same here
33+
case CharSequence cs -> "any other CharSequence";
3034

35+
case null -> "null =s";
3136
default -> "unhandled type";
3237
};
3338
}

java-18/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Simple Web Server</title>
8+
</head>
9+
<body>
10+
Hello world from Simple Web Server!
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)