Leveraging Java Type System to Represent Special States

Sergiy Yevtushenko
4 min readSep 3, 2021

As every Java developer know, variables with non-primitive types are initialized to null. This is the Java (and not just Java) way to say that "value is missing" or "variable has no value assigned yet".

There are often situations when we may return value, but sometimes we need to notify the caller that there is an error. The idiomatic Java way to do this is to throw an exception.

What described above situations have in common? They cover different cases and use different ways to represent/handle them.