Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created March 7, 2019 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/9d14a024c8cb9ed71ea737ed95bdd470 to your computer and use it in GitHub Desktop.
Save mcupak/9d14a024c8cb9ed71ea737ed95bdd470 to your computer and use it in GitHub Desktop.
Export of my JShell session from Exploring what's new in Java 10 and 11 (and 12) talk at DevNexus 2019.
List<Integer> list = new ArrayList<Integer>()
list.add(1)
list.add(2)
list.add(3)
list = Collections.unmodifiableList(list)
List<Integer> list2 = new ArrayList<Integer>(list)
List<Integer> list2 = Collections.unmodifiableList(new ArrayList<Integer>(list))
List<Integer> list = List.of(1,2,3)
list.add(4)
List<Integer> list2 = List.copyOf(list)
Set.copyOf(list)
list.stream().collect(toUnmodifiableList())
list.toArray(Integer[]::new)
String s = " hello\n "
s.repeat(10)
s.repeat(10)
.lines()
.forEach(System.out::println)
s.stripLeading()
s.stripTrailing()
s.strip()
s.isBlank()
s.repeat(10)
.lines()
.forEach(System.out::println)
s.repeat(10)
.indent(4)
.lines()
.forEach(System.out::println)
s.repeat(10)
.indent(-4)
.lines()
.forEach(System.out::println)
s.transform(s -> 123)
Reader.nullReader()
Writer.nullWriter()
InputStream.nullInputStream()
OutputStream.nullOutputStream()
Path path = Path.of("/tmp/hello")
Files.writeString(path, "hello devnexus")
Files.readString(path)
Files.isSameFile(path, path)
Files.mismatch(path, path)
HttpHandler handler = he -> {
String body = "hello devnexus";
he.sendResponseHeaders(200, body.length());
try (OutputStream os = he.getResponseBody()) {
os.write(body.getBytes());
}
}
/l handler
HttpServer hs = HttpServer.create(new InetSocketAddress(8000), 0)
hs.createContext("/hello", handler)
hs.start()
HttpClient hc = HttpClient.newHttpClient()
hc.version()
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8000/hello")).GET().build()
HttpResponse<String> response = hc.send(request, BodyHandlers.ofString())
response.statusCode()
response.body()
CompletableFuture<HttpResponse<String>> response = hc.sendAsync(request, BodyHandlers.ofString())
response.get().body()
var x = "hello"
/v x
var list = new ArrayList<String>()
/v list
int x
var x
var x = null
var x = (Integer) null
int x=0, y=0
var x=0, y=0
int inc(int x) { return x+1; }
int inc(var x) { return x+1; }
var inc(int x) { return x+1; }
for (var i=0; i<100; i++) {}
for (var i :list) {}
short x = 1
var x = 1
/v x
var x = 0x12345678
/v x
var x = 0x123456789
var x = 0x123456789l
class MyClass { var x = 1; }
var var = 1
void var() {}
class var {}
class Var {}
enum MyEnum { var }
var[] x = {1,2}
var x = {1,2}
var x = new int[]{1,2}
try { var x = 1; } catch (Exception ex) {}
try { } catch (var ex) {}
Supplier s = () -> ""
var s = () -> ""
Consumer c = (var x) -> System.out.println(x)
BiConsumer c = (var x, var y) -> System.out.println(x)
BiConsumer c = (var x, y) -> System.out.println(x)
BiConsumer c = (var x, Integer y) -> System.out.println(x)
BiConsumer c = (var x, var y) -> System.out.println(x)
/v c
Consumer c = System.out::println
var c = System.out::println
var list = new ArrayList<>()
/v list
var list = new ArrayList<>(Set.of(1))
/v list
var list = new ArrayList()
/v list
List<Integer> list1 = List.of(1)
/v list
/v list1
var x = list1.get(0)
/v x
List<?> list2 = list1
/v list2
var x = list2.get(0)
list2.add(x)
/v x
var x = new Object() { int a = 1; int b = 2; }
x.a
/v x
Object x = new Object() { int a = 1; int b = 2; }
x.a
<T extends Number & Comparable> T makeIntersection() { return null; }
var x = makeIntersection()
/v x
/env
int x = 0
switch (x) { case 0 -> 0; default -> 1; }
var y = switch (x) { case 0 -> 0; default -> 1; }
/v y
var y = switch (x) { case 0 -> 0; default -> "hello"; }
/v y
switch (x) { default -> System.out.println("hello"); }
var y = switch (x) { default -> System.out.println("hello"); }
var y = switch (x) { default -> { System.out.println("hello"); break "hello"; }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment