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
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
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
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?
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?
Records
• Convenient data stores
• Ideal for DTOs and classes without much business logic
• Far less boilerplate
• Lightweight constructor syntax
• Readability over lines of code
Pay Java records a visit!
115 W CHIPPEWA ST - DOWNTOWN BUFFALO, NY (corner of Delaware)
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