This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
record Point(int x, int y) {} | |
enum Color { RED, GREEN, BLUE } | |
record ColoredPoint(Point p, Color c) {} | |
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {} | |
static void printXCoordOfUpperLeftPointBeforePatterns(Rectangle r) { | |
if (r == null) return; | |
ColoredPoint ul = r.upperLeft(); | |
if (ul == null) return; | |
Point p = ul.p(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } | |
public class SwitchEnum { | |
// switch statement | |
public boolean isWeekendStatement(DayOfWeek day) throws NoSuchDayOfWeekException { | |
boolean isWeekend; | |
switch (day) { | |
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY: | |
isWeekend = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// without type patterns | |
if (obj instanceof String) { return ((String) s).length(); } | |
// type patterns, cast to String and assign to s | |
if (obj instanceof String s) { return s.length(); } | |
x instanceof String s && s.length() | |
// compile error, out of scope | |
x instanceof String s || s.length() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x is automatically cast to String | |
if (obj is String) { return obj.length } | |
x is String && x.length == 0 | |
// compile error | |
x is String || x.length == 0 | |
// with `when` expression | |
when (x) { | |
is String -> x.length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Shape | |
case class Point(x: Double, y: Double) {} | |
case class Square(topLeft: Point, side: Double) extends Shape {} | |
case class Rectangle(topLeft: Point, height: Double, width: Double) extends Shape {} | |
case class Circle(center: Point, radius: Double) extends Shape {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed interface Shape {} | |
record Point(double x, double y) {} | |
record Square (Point topLeft, double side) implements Shape {} | |
record Rectangle (Point topLeft, double height, double width) implements Shape {} | |
record Circle (Point center, double radius) implements Shape {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Square implements Shape { | |
private Point topLeft; | |
private double side; | |
public double area() { | |
return side * side; | |
} | |
} | |
public class Rectangle implements Shape { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Square { | |
public Point topLeft; | |
public double side; | |
} | |
public class Rectangle { | |
public Point topLeft; | |
public double height; | |
public double width; | |
} |