Java Records vs. Lombok

Java for a lot of time has been accused and mocked for its verbosity. Even the most passionate Java developers have to admit that it felt ridiculous to declare a bean class with two attributes. If you follow the right recommendations, you end up adding not only getters and setters, but also the implementations of toString hashcode and equals methods. The final result is a chunk of boilerplate that invites you to start learning another language. 

Java
 
import java.util.Objects;

public class Car {

   private String brand;
   private String model;
   private int year;

   public String getBrand() {
      return brand;
   }

   public void setBrand(String brand) {
      this.brand = brand;
   }

   public String getModel() {
      return model;
   }

   public void setModel(String model) {
      this.model = model;
   }

   public int getYear() {
      return year;
   }

   public void setYear(int year) {
      this.year = year;
   }

   @Override
   public String toString() {
      return "Car{" +
              "brand='" + brand + '\'' +
              ", model='" + model + '\'' +
              ", year=" + year +
              '}';
   }

   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      Car car = (Car) o;
      return year == car.year && Objects.equals(brand, car.brand) && Objects.equals(model, car.model);
   }

   @Override
   public int hashCode() {
      return Objects.hash(brand, model, year);
   }
}


WebFlux and Spring Data Example for Java Records

Traditionally, Java developers have depended on constructors, accessors, equals(), hashCode(), and toString() to define classes for basic aggregation of values. However,  this is an error-prone approach that adds little value and diverts attention away from modeling immutable data. Java records were initially offered as a preview in JDK 14 to simplify writing data carrier classes. The second preview was released in JDK 15, and the final version in JDK 16. The JDK Enhancement Proposal JEP 395 has a summary of this history.

While code generators can help reduce boilerplate code, the Java record proposals focus on the semantics of the code. Let's look at the characteristics and benefits of Java records and how to use them to develop a REST API and query a database.

Spring Boot and Java 16 Records

On March 16th, 2021, Java 16 was GA. With this new release, tons of new exciting features have been added. Check out the release notes to know more about these changes in detail. This article's focus will be on Java Records, which got delivered with JEP 395. Records were first introduced in JDK 14 as a preview feature proposed by JEP 359, and with JDK 15, they remained in preview with JEP 384. However, with JDK 16, Records are no longer in preview.

I have picked Records because they are definitely the most favored feature added in Java 16, according to this Twitter poll by Java Champion Mala Gupta.

Java Records: A Closer Look

Records is a new preview feature in Java 14, providing a nice compact syntax to declare classes that are supposed to be dumb data holders. In this article, we’re going to see what Records looks like under the hood. So buckle up!

Class Representation

Let’s start with a very simple example: