DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java String: A Complete Guide With Examples
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Import a Function/Module in Dataweave
  • Getting Started With Agentic Workflows in Java and Quarkus

Trending

  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • AI-Driven Integration in Large-Scale Agile Environments
  1. DZone
  2. Coding
  3. Java
  4. Java Code Style: Operators and New Lines [Snippet]

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.

By 
Sebastian Daschner user avatar
Sebastian Daschner
·
Feb. 23, 18 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

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

Operator (extension) Java (programming language)

Published at DZone with permission of Sebastian Daschner. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java String: A Complete Guide With Examples
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Import a Function/Module in Dataweave
  • Getting Started With Agentic Workflows in Java and Quarkus

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook