Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created Nov 24, 2018
Embed
What would you like to do?
JShell transcript from my Exploring what's new in Java in 2018 session at Devoxx Ukraine 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(new Integer[list.size()])
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()
Stream.of(s).filter(String::isBlank).forEach(System.out::println)
Stream.of(s).filter(s -> !s.isBlank()).forEach(System.out::println)
Stream.of(s).filter(not(String::isBlank)).forEach(System.out::println)
Optional.empty()
Optional.of("something")
Optional.empty().orElse("empty")
Optional.empty().orElseGet(() -> "empty")
Optional.empty().or(() -> Optional.of("empty"))
Optional.empty().orElseThrow()
Optional.empty().isPresent()
Optional.empty().isEmpty()
Reader.nullReader()
Writer.nullWriter()
InputStream.nullInputStream()
OutputStream.nullOutputStream()
Path path = Path.of("/tmp/hello_devoxx")
Files.readString(path)
Files.writeString(path, "sdfasdf")
Files.readString(path)
Path path2 = Path.of("/tmp/hello_devoxx")
Files.isSameFile(path, path2)
HttpHandler handler = he -> {
String body = "hello devoxx";
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()
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
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 Test { var }
var[] x = {1,2}
var x = {1,2}
var x = new int[]{1,2}
try { var x = 1; } catch (Exception ex) {}
try { var x = 1; } 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 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 makeIntersectionType() { return null; }
var x = makeIntersectionType()
/v x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment