10+ years Java developer
The best code is no code at all. This sentence has multiple meanings. One of the ways of interpreting it is that you should use third-party libraries. There are many advantages of libraries: you should not re-invent the wheel. Huge communities support them. Also, the code of libraries is high-performance and well tested. The Java ecosystem has been growing for 25 years, and you can find Java libraries for solving a lot of routine tasks.
Libraries, like Google Guava and Apache Commons, help to write code for typical routines. General-purpose libraries contain utility classes for working with beans, collections, caching, compression, hashing, I/O, cryptography, mathematics and statistics, string processing, graphs, concurrency, validation, and many more.
Guava was an internal Google project designed by Joshua Bloch, the lead designer of Java Collection Framework and author of the “Effective Java” book.
Previously, Guava was the only collections library with Java 5 features support, like generics, enums, covariant return types, etc. Today Guava is actively developed and maintained.
A wide range of projects uses Apache Commons. It comprises 44 modules, and you can add only the dependencies that you need. Since version 4.0, Apache Commons also supports generics.
If you are developing a large system, you need logging. There are several kinds of logging. It can help understand code behavior, capture errors, monitor and analyze program activity, and perform an audit of important events.
Historically, the most popular logging library was Log4j. Now, Log4j is obsolete, but it influenced modern logging libraries.
The original creator of Log4j, Ceki Gülcü, started new projects: Logback and SLF4J. Logback is a successor of Log4j and advantages, such as better performance, configuration files in Groovy, auto-reloading of configuration files, auto-removal of old log archives, auto-compression of archived log files, advanced filtering, and others. SLF4J (Simple Logging Facade) is an interface for logging libraries and does not do logging itself. With SLF4J, a developer can switch between logging implementations just by changing dependencies.
Apache Log4j2 is a modern logging library. It has all advantages of Logback. Also, Log4j2 provides many exciting features like plugin architecture, Java 8 lambda support, custom log levels, integration with application servers, and configuration through Spring Cloud Configuration.
Apache Commons Logging, a component of Apache Commons project, like SLF4J, provides API for logging and can be used with any implementation at runtime.
Writing tests is an integral part of software development. They help ensure that the code behaves in a certain way. Automatically running regression tests makes certain that changes, such as enhancements or fixes, do not break existing functionality or introduce new faults.
The most commonly used Java unit test framework is JUnit. JUnit allows writing repeatable tests in an annotation-based way and run them by a build automation tool. JUnit supports grouping test cases, parameterization, timeouts, customization of test names, and nesting of test cases.
TestNG (Test Next Generation) is also a popular testing framework and more advanced than JUnit. TestNG uses not only for unit testing but also for integration and functional testing. In contrast to JUnit, TestNG supports dependent tests, parallel test execution (JUnit provides it in experimental mode), and other features.
Usually, when you write a unit test, it covers the functionality of a single class. But it can depend on other classes, environment, or external systems. In tests, you can simulate these dependencies with mock objects. Mockito is the most popular mocking framework that helps to create mock objects and specify their behavior.
The critical element of any test is an assertion - checking that a particular condition is met. Assertion libraries help to write assertions. You can use Hamcrest, bundled in JUnit, or AssertJ, which provides fluent API, or another assertion library.
It is often necessary to exchange data between software components or with external systems. We can use human-readable (JSON, XML) or binary data formats for this purpose.
Jackson and Gson are the most popular libraries for working with JSON. Both of them support streaming processing and custom serialization. Jackson provides extensive support for annotations. Gson is easy to use in sample cases and is suitable when you do not have access to the source code for adding annotations.
For working with XML, you can use JAXB and StAX libraries. JAXB allows mapping Java objects to XML documents in a convenient way. StAX provides streaming processing of XML documents. StAX is suitable when an application works with huge XML documents.
Human-readable formats have a drawback - a heavy payload size. Protobuf (Google Protocol Buffers) provides a high-performance format for transmitting structured data.
This article aims not just to list the most common Java libraries but to show that you can easily find ready-to-use libraries for your projects. Using third-party libraries helps you be a more productive Java developer.
Create your free account to unlock your custom reading experience.