Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export of my JShell session from the Exploring the last year of Java talk at ConFoo 2020.
HttpHandler handler = he -> {
String body = "hello confoo";
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()
URI uri = URI.create("http://localhost:8000/hello")
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection()
c.setRequestMethod("GET")
c.getResponseCode()
new BufferedReader(new InputStreamReader(c.getInputStream())).readLine()
HttpClient client = HttpClient.newHttpClient()
client.version()
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build()
HttpResponse<String> response = client.send(request, BodyHandlers.ofString())
response.statusCode()
response.body()
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header("Content-Type", "plain/text")
.POST(BodyPublishers.ofString("hello"))
.build()
HttpResponse<String> response = client.send(request, BodyHandlers.ofString())
response.statusCode()
response.body()
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, BodyHandlers.ofString())
response.get().statusCode()
response.get().body()
int x = 0
switch (x) { case 0 -> 0; default -> 1; }
switch (x) { case 0,1 -> 0; default -> 1; }
switch (x) { default -> System.out.println("hello"); }
switch (x) { default -> { System.out.println("hello");yield "hello"; }}
switch (x) { default -> { System.out.println("hello");var yield = "hello"; yield yield; }}
Object o = ""
if (o instanceof String) {
String s = (String) o;
s.isEmpty();
}
if (o instanceof String s) {
s.isEmpty();
}
o instanceof String s && s.isEmpty()
if (!(o instanceof String s)) {
s.isEmpty();
}
o instanceof String s || s.isEmpty()
"""
{
"foo" : "bar"
}
"""
/l
"""
{
"foo" : "bar"
}
"""
"""
{
"foo" : "bar"
}"""
""""""
"""
"""
"""
{
"foo" : "bar"
}
"""
"""
{
"foo" : "bar" \s
}
"""
"""
{
"foo" : "bar" \
}
"""
"""
{
"foo" : "bar" \
}
"""
String s = " hello\n "
s.repeat(10)
s.repeat(10).lines().forEach(System.out::println)
s.stripLeading()
s.stripTrailing()
s.strip()
s.isBlank()
Character.isWhitespace(0)
s.repeat(10).indent(4).lines().forEach(System.out::println)
s.repeat(10).indent(-4).lines().forEach(System.out::println)
s.transform(s -> 123)
String s = " hello\n world"
System.out.println(s)
System.out.println(s.stripIndent())
"\n".equals("\\n".translateEscapes())
"hello %s".formatted("world")
record Tuple(int a, int b) {}
Tuple x = new Tuple(1,2)
x.toString()
x.a()
record Tuple(int a, int b) extends Number {}
record Tuple(int a, int b) implements Serializable {}
record Tuple(int a, int b) { int c; }
record Tuple(int a, int b) { static int c; }
abstract record Tuple(int a, int b) { static int c; }
record Tuple(int a, int b) {
void update() {
a = 3;
}
}
record Tuple(int a, int b) {
public Tuple {
System.out.println("validating");
}
}
Tuple x = new Tuple(1,2)
x.getClass().isRecord()
x.getClass().getRecordComponents()
x.getClass().getRecordComponents()[0].getName()
x.getClass().getRecordComponents()[0].getType()
x.getClass().getRecordComponents()[0].getAccessor()
RecordingStream rs = new RecordingStream()
String event = "jdk.CPULoad"
rs.enable(event).withPeriod(Duration.ofSeconds(1))
rs.onEvent(event, System.out::println)
rs.start()
FlightRecorder.getFlightRecorder().getEventTypes().stream().map(e -> e.getName()).forEach(System.out::println)
"""
hello %s
"""
"""
hello %s
"""+"sdfsd"
"""
hello %s
""".formatted("sdfsd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment