Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created November 30, 2018 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/45ec3007058426e29d0e8b1d31493521 to your computer and use it in GitHub Desktop.
Save mcupak/45ec3007058426e29d0e8b1d31493521 to your computer and use it in GitHub Desktop.
JShell transcript from my Exploring what's new in Java 10 and 11 session at Codemotion Milan 2018.
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)
list2.add(4)
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)
list2.add(4)
Set.copyOf(list)
list.stream().collect(toUnmodifiableList())
list.toArray(Integer[]::new)
String s = " hello\n "
s.repeat(10)
s.repeat(10).lines()
s.repeat(10).lines().forEach(System.out::println)
s.stripLeading()
s.stripTrailing()
s.strip()
s.isBlank()
Reader.nullReader()
Writer.nullWriter()
InputStream.nullInputStream()
OutputStream.nullOutputStream()
Path path = Path.of("/tmp/test")
Files.writeString(path, "hello")
Files.readString(path)
HttpHandler handler = he -> {
String body = "hello codemotion";
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.body()
response.get().body()
String x = "hello"
var x = "hello"
/v x
var list = new ArrayList<String>()
/v list
int x
var x
var x = null
var x = (Integer) null
short x = 1
var x = 1
/v x
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; }
class MyClass { var x = 1; }
var var = 1
void var() {}
class var {}
class Var {}
enum Test { var }
var[] x = {1,2}
var x = {1,2}
var x = new int[]{1,2}
try {} catch (var ex) {}
Supplier s = () -> ""
var s = () -> ""
Consumer c = x -> System.out.println(x)
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)
Consumer c = System.out::println
var c = System.out::println
var list = new ArrayList<>()
/v list
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment