The document discusses Java records, which were introduced in Java 14 to provide a simpler way of creating classes that mainly store and retrieve data. Records reduce boilerplate code by automatically generating common methods like constructors, getters, equals, hashCode and toString. They model data as data rather than objects, making the code easier to read and less error-prone. The document demonstrates how to define and use records, customize their behavior, and convert an existing class to a record for simplification. It argues that records are well-suited for data transfer objects and other classes without significant business logic.
13. Some Inconveniences
• Not perfect for classes that are just wrappers for some data
• Lots of boilerplate: constructors, accessors, equals,
hashCode, toString
• No fun to write => Delegate to the IDE
• Reading does not feel like reading developer’s intent
15. The Pair Awakens
• Part of the javafx.util package
• Pair<K, V> pair = new Pair<>(K key, V value)
• K getKey( ), V getValue( )
• It gets you a standard implementation of equals,
hashCode, and toString
18. Well, not exactly
• The notion of type names is lost, only key-value pair
gets printed out
• JavaFX has been removed from JDK 11
• Its module/jar has to be included on each compilation
and runtime
• Overhead saved from the Person class => traded with
some overhead in configuration
20. Rise of the Record
• record Person (String name, int age) { }
• Transparent holders for shallowly immutable data
• Modeling data as data
• The data, the whole data, and nothing but the data
What are records?
21. Living with records
• Boilerplate reduction, obviously
• Good semantics => easier to read, less error prone
• Defaults for constructor, getters, equals, hashCode,
and toString
• Extension by methods, custom constructor
What do they offer?
31. Records
• Convenient data stores
• Ideal for DTOs and classes without much business logic
• Far less boilerplate
• Lightweight constructor syntax
• Readability over lines of code
33. Pay Java records a visit!
115 W CHIPPEWA ST - DOWNTOWN BUFFALO, NY (corner of Delaware)
34. If you have any questions
Please reach out directly after the talk in person
or leave me a message @albihasani94 on twitter
Albin Hasani
Twitter: @albihasani94