Over a million developers have joined DZone.

Java Code Style: Operators and New Lines [Snippet]

DZone's Guide to

Java Code Style: Operators and New Lines [Snippet]

Ever wonder whether operators should go on the current line or a new one? Let's see what the Google Java Style Guide has to say.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Last week, I discovered what helped me decide on what I have struggled with for a long time: whether to place operators such as &&||, or + on the same or next line.

When constructing boolean expressions or concatenating Strings, we might need to break long lines. According to the Google Java Style Guide, the break should come “before the symbol”, therefore:

String veryLong = "This is an example of a very, very, very, very, very, very, "
        + "very, very long String";

boolean found = strings.stream()
        .anyMatch(s -> s.startsWith("foo"));

if (someLogic() && someOtherLogic() && someMoreComplexLogic()
        || found)
    // ...

As you can see in this example, the +||, and also . operators are placed on the new line, before the next symbol. The exceptions to the rules are assignment operators, open parenthesis, commas, and lambda arrows.

I think this guide is consistent, makes sense and solves my problem to think about which one to go with over and over again

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

java ,code style ,operators ,code formatting

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}