DEV Community

Cover image for [Tiny] Trailing commas in Java
Petr Filaretov
Petr Filaretov

Posted on

[Tiny] Trailing commas in Java

As you may already know, Kotlin (and many other programming languages) has a nice feature called trailing comma. This is a comma symbol after the last item of a series of elements.

But do you know that Java supports trailing commas too? The support is very limited but still worth noting.

You can use trailing commas in two cases:

  • Array initialisers:
int[] answers = new int[]{
        42,
        7 * 6,
        14 * 3,
        21 * 2,
        7 * 3 * 2, // trailing comma
};
Enter fullscreen mode Exit fullscreen mode
  • Enums:
enum Answer {
    YES,
    NO,
    MAYBE,
    FORTY_TWO, // trailing comma
}
Enter fullscreen mode Exit fullscreen mode

Dream your code, code your dream.

Top comments (0)