Java Records vs POJO
In Java, records were introduced in JDK 14 as a new type of class to represent immutable data. They provide a more concise syntax for defining classes whose primary purpose is to hold data, similar to POJOs (Plain Old Java Objects), but with some important differences.
One of the main advantages of records over POJOs is that they reduce the amount of boilerplate code needed to define a class. For example, a POJO might require a constructor, getters, setters, equals, hashCode, and toString methods, all of which must be manually defined. With records, all of these methods are automatically generated based on the record’s fields.
Here is an example of a simple record definition:
public record Person(String name, int age) {}This defines a record called Person with two fields: name of type String and age of type int. The record automatically provides a constructor, getters for the fields, an equals method, a hashCode method, and a toString method.
To use a record, you can create an instance like this:
Person person = new Person("Alice", 30);You can access the fields using the getters:
System.out.println(person.name()); // prints "Alice"
System.out.println(person.age()); // prints 30You can also create a new record instance with updated fields using the with (note: it is removed in java 17) method:
Person olderPerson = person.withAge(40);
System.out.println(olderPerson); // prints "Person[name=Alice, age=40]"Overall, records provide a simpler and more concise way to define classes whose primary purpose is to hold data. However, they may not be suitable for all use cases, especially if you need to define behavior or mutable state. In those cases, you may still need to use traditional classes or POJOs.
Get Eltac Shixseyidov’s stories in your inbox
Join Medium for free to get updates from this writer.
Some other key differences between records and POJOs include:
- Records are immutable by default. This means that once a record object is created, its values cannot be changed. This can be useful for representing data that should not change once it is created. POJOs, on the other hand, are mutable by default and require additional effort to make them immutable.
- Records do not support inheritance, whereas POJOs can be extended to create subclasses. Instead, records can use the
sealedkeyword to define a limited set of possible subtypes. - Records require that all their fields be declared in the constructor parameter list, whereas POJOs can have fields that are set using setters or other methods. This can make it easier to understand what data a record is holding.
- Records have a more limited set of methods than POJOs, but the methods they do have are automatically generated and can be customized using annotations.
Overall, records can be a useful tool for simplifying code and reducing boilerplate when you are working with immutable data. However, they are not a silver bullet and may not be the right choice for every situation. As with any programming tool, it is important to weigh the pros and cons carefully and choose the approach that best fits your needs.
Another important consideration when deciding whether to use records or POJOs is compatibility with older versions of Java. Records were introduced in JDK 14, which means that they are not available in earlier versions of Java. If you are developing a library or application that needs to run on multiple versions of Java, you may need to use POJOs or other alternatives that are compatible with older versions.
It is also worth noting that records are a relatively new feature in Java, which means that some developers may not be familiar with them yet. If you are working on a team, it is important to ensure that everyone is comfortable with records and understands how to use them effectively.
In summary, records are a new feature in Java that provide a simpler and more concise way to define classes for holding immutable data. They can be a useful tool for reducing boilerplate and making code more readable, but they are not a one-size-fits-all solution and may not be suitable for all use cases. When deciding whether to use records or POJOs, it is important to consider factors such as compatibility with older versions of Java, team familiarity with records, and the specific needs of your application or library.
Records can be a good choice for representing endpoint request data, particularly when the request data is simple and has a fixed set of fields. For example, if you have an API endpoint that accepts data for creating a new user, you might define a record like this:
public record CreateUserRequest(String username, String password, String email) {}This record defines a request for creating a new user with three fields: username, password, and email. The record is immutable by default, which means that once it is created, its fields cannot be changed.
Using a record like this has a few advantages over other approaches. First, it makes the code for handling the request more concise and easier to read. Instead of manually parsing and validating the request data, you can simply accept an instance of the CreateUserRequest record and assume that its fields are valid.
Second, because records are immutable by default, you don’t need to worry about the request data being modified during processing. This can make the code more reliable and easier to reason about.
Finally, because records are a new feature in Java, using them for endpoint requests can demonstrate that your codebase is up-to-date and taking advantage of the latest features.
Of course, there are some potential drawbacks to using records for endpoint requests. For example, if the request data has many optional or nested fields, a record may not be the best choice. In those cases, a more flexible data structure like a POJO or JSON object might be more appropriate.
Overall, using records as endpoint requests can be a good choice in many cases, particularly for simple request data with a fixed set of fields. It is worth considering whether records would be a good fit for your specific use case.
For support: buymeacoffee
Follow me to get more blog updates!











