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?

原文链接:Get Started with Modern Java Today: A Concise Guide

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容