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

Sean Evans @seanevans

About: Engineer

Joined:
Nov 16, 2022

Get Started with Modern Java Today: A Concise Guide

Publish Date: Dec 19 '24
21 5

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?

Comments 5 total

  • Ravin Rau
    Ravin RauDec 19, 2024

    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! 🚀

  • Karl Heinz Marbaise
    Karl Heinz MarbaiseDec 31, 2024

    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...

    • Sean Evans
      Sean EvansJan 2, 2025

      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.

      • Karl Heinz Marbaise
        Karl Heinz MarbaiseJan 2, 2025

        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...

Add comment