DEV Community

Cover image for Get Started with Modern Java Today: A Concise Guide
Sean Evans
Sean Evans

Posted on

8 3 1 4 3

Get Started with Modern Java Today: A Concise Guide

Many people think Java is a verbose language. I've heard a joke that if your employer pays you by lines of code, then Java is the way to go.

With updates to Java, modern Java isn't so verbose anymore, so check it out.

If you're starting a new project, use modern Java today.

1. Use record instead of lombok

βœ… Use record

public record User(String id, String name) {}
Enter fullscreen mode Exit fullscreen mode

πŸ™… Instead of lombok annotations

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String id;
    private String name;
}
Enter fullscreen mode Exit fullscreen mode

πŸ™… And instead of getter, setter, equals(), hashCode(), toString(), and etc.

public class User {
    private String id;
    private String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    // equals(), hashCode(), toString() and etc. are omitted below.
}
Enter fullscreen mode Exit fullscreen mode

2. Use var instead of class name

βœ… Use var

var user = new User("1", "Tom");
Enter fullscreen mode Exit fullscreen mode

πŸ™… Instead of duplicated class name

User user = new User("1", "Tom");
Enter fullscreen mode Exit fullscreen mode

3. Use STR."\{}" instead of + between strings

βœ… Use STR."\{}"

var firstName = "Thomas";
var lastName = "Cat";
var fullName = STR."His name is \{firstName} \{lastName}.";
Enter fullscreen mode Exit fullscreen mode

πŸ™… Instead of + and " "

var firstName = "Thomas";
var lastName = "Cat";
var fullName = "His name is " + firstName + " " + lastName + ".";
Enter fullscreen mode Exit fullscreen mode

πŸ™… And instead of new StringBuilder() and append()

var firstName = "Thomas";
var lastName = "Cat";

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("His name is ").append(firstName).append(" ").append(lastName).append(".");

var fullName = stringBuilder.toString();
Enter fullscreen mode Exit fullscreen mode

4. Use """ instead of "\n"+ between multiline strings

βœ… Use """

var html = """
    <html>
    <body>
      <p>Hello World!</p>
    </body>
    </html>
    """;
Enter fullscreen mode Exit fullscreen mode

πŸ™… Instead of "\n" and +

var html =
    "<html>\n" +
    "<body>\n"+
    "  <p>Hello World!</p>\n"+
    "</body>\n"+
    "</html>\n";
Enter fullscreen mode Exit fullscreen mode

5. Use .of() instead of .add() when to init a list

βœ… Use List.of()

var list = List.of(1, 2, 3);
Enter fullscreen mode Exit fullscreen mode

πŸ™… Instead of Stream

var list = Collections.unmodifiableList(Stream.of(1, 2, 3).collect(toList()));
Enter fullscreen mode Exit fullscreen mode

πŸ™… And instead of new ArrayList<>() and .add()

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list = Collections.unmodifiableList(list);
Enter fullscreen mode Exit fullscreen mode

Conclusion

I've listed the 5 modern Java features I use most often. And using them gives me the joy of Java programming. 🍹

The official names of these features are:

  1. JEP 395: Records
  2. JEP 286: Local-Variable Type Inference
  3. JEP 430: String Templates (Preview)
  4. JEP 378: Text Blocks
  5. JEP 269: Convenience Factory Methods for Collections

By the way, the examples above are based on JDK 21, the latest LTS version.

What are your favorite modern Java features?

Image of Timescale

πŸš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsβ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (5)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise β€’

Unfortuantely the usage of 3. Use STR."\{}" instead of does not make sense, because String Templates have been removed from recent JDK versions... neither JDK 23 openjdk.org/projects/jdk/23/ nor JDK 24 openjdk.org/projects/jdk/24/ will have String Templates in it... furthermore to use a preview feature you have to enable that explicitly...

Collapse
 
seanevans profile image
Sean Evans β€’ β€’ Edited

Yes, thanks for adding that fact. As I said at the end of the article, the article is based on JDK 21, and the string template is a feature in preview.
I hope the string templates will be released in next LTS version of JDK.

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise β€’

As I mentioned in JDK 24 it is not in ... so in the JDK 25 it will be very likely not in as well because there is not yet a new JEP for that...

Collapse
 
juniourrau profile image
Ravin Rau β€’

Great guide on modern Java features! It's clear and helpful for beginners. To improve, consider adding a section on how these updates impact performance. Keep up the great work! πŸš€

Collapse
 
seanevans profile image
Sean Evans β€’

Thank you!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more