CKTIS / Java
default — fallback branch or implementation
default — fallback branch or implementation: default marks an unmatched switch branch, an interface method with an implementation, or an annotation element’s default value.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
defaultdefault marks an unmatched switch branch, an interface method with an implementation, or an annotation element’s default value.
Use default in a switch expression when the input space is not otherwise exhaustive; an interface default method can evolve an API without breaking implementors.
A switch default can hide an unhandled new value, and a class inheriting conflicting default methods must resolve the conflict explicitly.
Object methods do not become interface default methods; an annotation default belongs to the element declaration rather than being stored at every use.
The inherited default method prints item, while the missing case for 7 sends the switch to default and produces other.
Examples
public class Main {
interface Named {
default String label() { return "item"; }
}
static class Item implements Named {}
public static void main(String[] args) {
String category = switch (7) {
case 0 -> "zero";
default -> "other";
};
System.out.println(new Item().label());
System.out.println(category);
}
}
Result
item other
The inherited default method prints item, while the missing case for 7 sends the switch to default and produces other.