DEV Community

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

Posted on

3

[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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay