Runnable sources, broken examples and captured transcripts for the ankurm.com JEP 512 article, including the measured launch order and a private-constructor difference between 25 and 27. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
26 lines
928 B
Java
26 lines
928 B
Java
// A compact source file is a whole class body: fields, methods, and nested types all sit at the top level.
|
|
// Nothing here is imported. List, Map, TreeMap, Collectors and BigDecimal all live in java.base.
|
|
String greeting = "hello";
|
|
int calls = 0;
|
|
|
|
record Point(int x, int y) {
|
|
double distance() { return Math.sqrt(x * x + y * y); }
|
|
}
|
|
|
|
String shout(String s) {
|
|
calls++;
|
|
return s.toUpperCase() + "!";
|
|
}
|
|
|
|
void main() {
|
|
List<String> words = List.of("banana", "apple", "cherry");
|
|
Map<String, Integer> lengths = new TreeMap<>();
|
|
for (String w : words) lengths.put(w, w.length());
|
|
IO.println(shout(greeting));
|
|
IO.println(words.stream().sorted().collect(Collectors.joining(", ")));
|
|
IO.println(lengths);
|
|
IO.println(new BigDecimal("1.10").add(BigDecimal.ONE));
|
|
IO.println(new Point(3, 4) + " is " + new Point(3, 4).distance() + " from the origin");
|
|
IO.println("calls so far: " + calls);
|
|
}
|