AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up.

utoValue is one of Google’s small open-source project. It is determined to help us reduce the amount of template code in Java and significantly improve productivity.

In the following article, I will introduce several common uses of AutoValue, its advantages and disadvantages, and a comparison of Lombok.

What’s AutoValue?

AutoValue — Generated immutable value classes for Java 7+ — from doc

For Java developers, we deal with eqauls, hascode, toString every day.

Nowadays, we already have tools like IDE, Lombok to help reduce the workload of writing these methods manually. But I still want to use AutoValue for a change.

AutoValue can restrict my freedom, help me write immutable value classes, and make fewer mistakes. **Value Class **is these Class just has a set of fields(values) and no logic.

Immutable is an important idea. The most significant advantage is that multi-threaded access can save a lot of synchronization control, because they are immutable, once the construction is completed, there will be no multi-threaded contention access problem.

The most troublesome processing of multithreading is to control the reading and writing problems. If everyone is reading, then there is no control, so a lot of synchronization operations are omitted.

In Java, concurrency is the most difficult to deal with, the most difficult to debug, and the most difficult to control performance.

Immutable can help us avoid concurrent writing, which naturally reduces the chance of errors and improves performance. But Immutable will also increase GC overhead because using replication will create more objects.

I list the most useful functions of AutoValue:

  • Generate bloated code like getter, setter, toString, equals, hashcode
  • Null value check
  • Gson serialization(auto-value-gson extension)

If you write Andriod App, you may find some other functions like Parcelable very useful, since I don’t, so I’ll just skip that part.

#java #open-source #programming

Why I Replace Lombok with Google AutoValue?
12.70 GEEK