cxxr /better-java

Resources for writing modern Java

★ 5,738⑂ 705👁 343⑃ 43● 24▤ 0
NOASSERTIONCreated 2014-05-05
+0
Daily
5,738
stars
705
forks
343
watchers
43
prs
24
issues
0
releases

README

Note: I'm working on version 2 of this guide and I need your help! Please use this form to give me feedback on what you think should go in the next version. Thanks!


 Better Java

Java is one of the most popular programming languages around, but no one seems
to enjoy using it. Well, Java is actually an alright programming language, and
since Java 8 came out recently, I decided to compile a list of libraries, 
practices, and tools to make using Java better. "Better" is subjective, so I
would recommend taking the parts that speak to you and use them, rather than
trying to use all of them at once. Feel free to submit pull requests 
suggesting additions.

This article was originally posted on 
my blog.

Read this in other languages: English, 简体中文

 Table Of Contents

 Style
   Structs
     The Builder Pattern
     Immutable Object Generation
   Exceptions
   Dependency injection
   Avoid Nulls
   Immutable-by-default
   Avoid lots of Util classes
   Formatting
     Javadoc
   Streams
 Deploying
   Frameworks
   Maven
     Dependency Convergence
   Continuous Integration
   Maven repository
   Configuration management
 Libraries
   Missing Features
     Apache Commons
     Guava
     Gson
     Java Tuples
     Javaslang
     Joda-Time
     Lombok
     Play framework
     SLF4J
     jOOQ
   Testing
     jUnit 4
     jMock
     AssertJ
 Tools
   IntelliJ IDEA
     Chronon
   JRebel
   The Checker Framework
   Code Quality
   Eclipse Memory Analyzer
 Resources
   Books
   Podcasts
   Videos

 Style

Traditionally, Java was programmed in a very verbose enterprise JavaBean style.
The new style is much cleaner, more correct, and easier on the eyes.

 Structs

One of the simplest things we as programmers do is pass around data. The
traditional way to do this is to define a JavaBean:

java
public class DataHolder {
    private String data;

    public DataHolder() {
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return this.data;
    }
}


This is verbose and wasteful. Even if your IDE automatically generated this
code, it's a waste. So, [don't do this][dontbean].

Instead, I prefer the C struct style of writing classes that merely hold data:

java
public class DataHolder {
    public final String data;

    public DataHolder(String data) {
        this.data = data;
    }
}


This is a reduction in number of lines of code by a half. Further, this class
is immutable unless you extend it, so we can reason about it easier as we know
that it can't be changed.

If you're storing objects like Map or List that can be modified easily, you
should instead use ImmutableMap or ImmutableList, which is discussed in the 
section about immutability.

 The Builder Pattern

If you have a rather complicated object that you want to build a struct for,
consider the Builder pattern.

You make a static inner class which will construct your object. It uses
mutable state, but as soon as you call build, it will emi

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON