Введение в программирование
Улучшения языка
BiFunction<String, Long, String> f = (String s, Long v) -> { return s + v; }
BiFunction<String, Long, String> f = (String s, Long v) -> s + v;
BiFunction<String, Long, String> f = (s, v) -> s + v;
Function<String, String> f = s -> s + s;
Supplier<String> s = () -> "!";
Consumer<String> c = s -> System.out.println(s + s);
Function<String, Integer> f1 = Integer::parseInt; // x -> Integer.parseInt(x);
Function<Integer, String> f2 = Integer::toString; // x -> x.toString();
Integer i = 2; // Не обязательно final Supplier<String> f3 = i::toString; // () -> i.toString();
Function<String, StringBuilder> f = StringBuilder::new; // s -> new StringBuilder(s);
/* final */ String hello = "Hello, "; Function<String, String> greeter = name -> hello + name;
// Не работает, так как write бросает IOException Consumer<String> c = writer::write;
collection.forEach(e -> { if (e.equals("done")) { // Что делать? } });
var hello = "world";
var counters = new HashMap<String, Integer>();
var hello;
var hello = null;
var counters = new HashMap<String, Integer>(); counters = Map.copyOf(counters);
java HelloWorld.java
java --class-path=.. HelloWorld.java
#!/usr/bin/java --source 11 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
class HelloWorld { public static void main(String[] args) { greeter.Greeter.greet(args[0]);
package greeter; public class Greeter { public static void greet(String name) { System.out.println("Hello, " + name);
int n = ...; switch (n) { case 0 -> System.out.println("zero"); case 1 -> System.out.println("one"); case 2 -> System.out.println("two"); default -> { System.out.println("too many"); System.out.println("many"); } }
int n = ...; System.out.println(switch (n) { case 0 -> "zero"; case 1 -> "one"; case 2 -> "two"; default -> { System.err.println("to"); yield "many"; } });
String code = "function hi() {\n" + "console.log('\"Hello, world!\"');\n" + "}\n" + "\n" + "hi();\n";
String code = """ function hi() { console.log('"Hello, world!"'); } hi(); """;
// helloworld String hello = """ hello\ world """;
// hello world String hello = """ hello\s\ world""";
record Point(int x, int y) {}
Point p = new Point(10, 20); System.out.println(p.x()); // True System.out.println(p.equals(new Point(10, 20));
record Range(int lo, int hi) { Range(int lo, int hi) { assert lo < hi : "Invalid range"; this.lo = lo; this.hi = hi; } }
record Range(int lo, int hi) { Range { assert lo < hi : "Invalid range"; } }
class Point { int x, y; public boolean equals(Object obj) { if (obj instanceof Point that) { return this.x == that.x && this.y == that.y; } return false; } }
Point that = (Point) obj;
class Point { int x, y; public boolean equals(Object obj) { return obj instanceof Point that && this.x == that.x && this.y == that.y; } }
if (объект instanceof Тип имя && /*...+*/) { /*...+*/ } else { /*...−*/ }
if (!(объект instanceof Тип имя) || /*...+*/) { /*...−*/ } else { /*...+*/ }
if (obj instanceof Point(int x, int y)) { return this.x == x && this.y == y; } return false;
if ( obj instanceof Rect(Point(int x1, int y1), Point p2) ) { System.out.format("(%d, %d) %s", x1, y1, p2)); }
static String format(Object obj) { return switch (obj) { case Integer i -> String.format("int %d", i); case Double d -> String.format("double %f", d); // When case Long l when l > 0 -> String.format("+long %d", l); case Long l when l < 0 -> String.format("-long %d", l); // Record case Point(int x, int y) -> String.format("Point(%d, %d)", x, y); case null -> null; default -> obj.toString(); }; }
sealed interface Shape permits Rect, Circle, Line {
sealed class Inline { final class Emphasis extends Inline {} final class Strong extends Inline {} final class Text extends Inline {} }
for (Shape _ : shapes)
} catch (NumberFormatException _) {
BiFunction<String, String, Integer> ZERO = (_, _) -> 0;
obj instanceof Point(int _, int _)