Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export of my JShell session for the Writing clean code with modern Java session at ConFoo 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.getClass()
Map.ofEntries(entry(1, "hello"))
List<Integer> list2 = List.copyOf(list)
list2.add(4)
Set.copyOf(list)
list.stream().collect(toUnmodifiableSet())
interface MyInterface {}
class MyClass implements MyInterface {}
new MyClass()
interface MyInterface {
default void defaultMethod() {
System.out.println("default method");
}
}
new MyClass().defaultMethod()
interface MyInterface {
default void defaultMethod() {
privateMethod():
System.out.println("default method");
}
private void privateMethod(){
System.out.println("private method");
}
}
interface MyInterface {
default void defaultMethod() {
privateMethod();
System.out.println("default method");
}
private void privateMethod(){
System.out.println("private method");
}
}
new MyClass().defaultMethod()
new MyClass().privateMethod()
/env
int x = 0
switch (x) { case 0 -> 0; default -> 1; }
switch (x) { case 0,1 -> 0; default -> 1; }
switch (x) { default -> System.out.println(x); }
switch (x) { default -> { System.out.println(x); break x; }}
var x = "hello"
/v x
int x
var x
var x = null
var x = (Integer) null
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; }
try {} catch (var ex) {}
var x = new int[]{1,2}
var x = {1,2}
var[] x = {1,2}
short x =1
var x =1
/v x
Consumer c = (var x) -> System.out.println(x)
var c = (var x) -> System.out.println(x)
var list = new ArrayList<String>()
/v list
var list = new ArrayList<>()
/v list
List<?> list = List.of(1)
/v list
var x = list.get(0)
list.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
var var = 1
;
void var() {}
class var{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment