From the course: Pattern Matching for Switch in Java 21

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Using record patterns with switch

Using record patterns with switch - Java Tutorial

From the course: Pattern Matching for Switch in Java 21

Using record patterns with switch

- [Instructor] Before learning about record patterns with switch, let's quickly look at what a record is. The record feature was first introduced in Java 14, and eventually became officially available in Java 16. A record is an immutable data class that requires only the type and the name of fields for it to be declared. If you are wondering what immutable means, an immutable class in Java means that once an object is created, its content or data cannot be changed. For example, all wrapper classes, like Integer, Boolean, Byte, Short, and String, in Java are immutable. So a record is similar to these because it is immutable. All the boilerplate code, like equals, hashCode, to string methods, as well as getters, and setters, and constructor is generated by the Java compiler. By default, the fields in a record are final. This is what makes a record immutable. Records are great as data transfer objects or data carriers. Having talked very quickly about Java records, this is an example of…

Contents