A null value can appear anywhere in a Java application. In this post, we’ll see how to work with data you control as well as data you don’t. We’ll also discuss how we can use annotation libraries like JSR 380, Spring annotations, Lombok, and some patterns to handle null values.

Dealing with nulls: Old way

Assertions

Java’s built-in assert keyword can be used to check for assumptions. It can be a handy utility to check for nulls:

The above example checks if the argument of foo is null. If it does, then it will fail by throwing an AssertionError with a message: Exception in thread…


The Twelve-Factor application methodology is a collection of best practices that are designed to enable applications to be developed with portability and resilience when deployed. In this post, we’ll cover the third factor, Config. We’ll also see how the spring ecosystem contributes to helping developers achieve this factor.

The Twelve-Factor application manifesto advocates on externalizing configs(passwords, hostnames…) as environment variables:

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code

Open-sourcing your codebase without compromising any credentials is the test that your app…


Introduction

Greetings!

Since Java 8, Oracle completely rebuilt its Date/Time API. The new API is supposed to replace the old one.

The new API which is located in the java.time package is thread-safe because most of the new classes are immutable, meaning that, after the object is constructed, it cannot be modified. This is especially useful when working in a multi-threaded environment where issues like thread interference and data corruption cannot happen thanks to immutability.

This article shows how to work with the new API.

Creating Dates and Times

Java lets us create dates and times using static factory methods. Note that you cannot create…


Introduction

Greetings!

Specifying the generic type allows Java to perform type checking at compile-time. But when using generics in your code, due to “Type Erasure” that occurs at compile-time, generic type parameters are converted to the Object type. This makes generic type parameters unable to call other methods except for the Object ones.

What if we want to invoke methods other than that those in Object class? This article explains what “Wildcards”, “bounds” and “Type Erasure” are. And how to use Wildcards to increase flexibility when using generics.

Type Erasure

Wikipedia defines Type erasure as:

Type erasure is the load-time process by which…


What is Thread safety? To save you the time from looking up to Wikipedia here is the definition:

Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.

What does this definition really mean? How can we correctly write thread-safe code? What are some of the constructs that Java offers to write such code?

Accessing shared data

Access to mutable shared data requires synchronization which can be implemented using one of these approaches:

  • Java’s built-in synchronized keyword


Photo by JESHOOTS.COM on Unsplash

1. Introduction

The strategy design pattern is one of the most widely used patterns in the software development world. In this article, we’re going to learn about the strategy design pattern, when to use it, when not, how we can leverage the pattern to make our design flexible? And we are going to see an example of how to implement it with and without lambdas.

2. Strategy Design Pattern

2.1 Definition

To save you the time spent on searching Wikipedia here is the definition:

The strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of…


1. Introduction

When working with custom objects, we often want to compare them based on some pre-defined criteria. That’s why we use the Comparable interface to implement a natural ordering for our custom objects.

Also, we often need to specify a total ordering if there is no natural ordering implemented by our custom object or override the natural ordering defined by the object, so we use a Comparator interface to do such a thing.

2. The Example Demo

In the example demo, we’ll show how to compare Person objects using natural ordering implemented by the Comparable interface and also to override that natural ordering by using…


1. Introduction

In this article, we’re going to take a peek at Maven Build Profiles. Build profiles are often underestimated yet they have a lot of capabilities, and we are going to explore some of its capabilities. How we can use it? how we can leverage build profiles to make our build flexible?

2. Overview of Maven Build Profiles

The power of a profile comes from its ability to modify the basic POM only under certain circumstances. A profile is an alternative set of values that set or override default values.

2.1 Why Use Profiles?

  • Profiles allow you to customize a particular build for a particular environment (development, testing, and production).
  • Profiles…


1. Introduction

Maven is a build automation tool primarily used for Java projects, but with a little configuration, Maven can also be utilized to build and manage projects written in Kotlin, Groovy, Scala and other programming languages. Maven is so versatile, you can leverage it to automate tasks such as managing and downloading dependencies, putting additional jars on a classpath, compiling source code into bytecode, running tests, packaging compiled code into deployable artifacts such as JAR, WAR, and deploying these artifacts to an application server or repository.

At this time of writing, Maven is the most popular build automation tool for Java…


1. What is AOP

To save you the time from looking up to Wikipedia here is the definition:

aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a “pointcut” specification, such as “log all function calls when the function’s name begins with ‘set’ ”.

Don’t worry If you don’t understand the definition just read through the whole article and you’ll find an explanation for every concept in this definition.

Hamza Belmellouki

Hamza(@HamzaLovesJava) is a self-taught developer and Java enthusiast. In his free time, he likes to blog about various technical topics at https://hamza-jvm.me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store