Java 8 Optional - Value Based Class Usage Guidelines With Examples

In this article, we will go through the use of Java 8 Optional class and understand what a Value-Based class is. What are the scenarios for using a value-based class? and finally, see the usage guidelines for Optional.

Java 8 Optional

Optional is a container object which may or may not contain a non-null value. You can read more about Optional here. There has been some improvements in Optional class in Java 9, read about them here.

Why use Optional Class?

  • Promotes better and cleaner design and code
  • It declares the purpose and the intent of the variable.
  • Reduces testing effort
  • Avoids NullPointerException
  • It supports Fail Fast approach of programming
  • Removes boiler plate conditional checks.
  • Supports fluent style
  • Promotes the use of functional style of programming.

Optional is a value-based class. We will cover the value-based class later in this article.

Value Types

Before we delve into value-based class lets understand what a value-type is? A value type is a data type which, is designed for being passed by value in and out of methods, and stored by value in data structures. The only value types which the Java language directly supports are the eight primitive types.

In the article “State of the Values” Brian Goetz proposed that Java VM and language can be gently extended with a new kind of type, a value type, which usefully combines the properties of Java’s current classes and primitive types, and these should be small immutable & identity-less.

A value type should be synonymous to “Codes like a class, works like an int”.

Value-Based Class

The concept of value type formed the basis of Value-Based classes. A boxed form of every value type will be a value-based class.

A Value-Based class is a Container class which is a wrapper for value-based types, where the instances of Value-based classes follow following principle (from Java Documentation):

  • are final and immutable (though may contain references to mutable objects)
  • have implementations of equals, hashCode, and toString which are computed solely from the instance’s state and not from its identity or the state of any other object or variable
  • make no use of identity-sensitive operations such as reference equality (==) between instances, identity hash code of instances, or synchronization on an instances’ intrinsic lock
  • are considered equal solely based on equals(), not based on reference equality (==)
  • do not have accessible constructors, but are instead instantiated through factory methods which do not commit as to the identity of returned instances
  • are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior.

A program may produce unpredictable results if it attempts to distinguish two references to equal values of a value-based class, whether directly via reference equality or indirectly via an appeal to synchronization, identity hashing, serialization, or any other identity-sensitive mechanism. Use of such identity-sensitive operations on instances of value-based classes may have unpredictable effects and should be avoided.

Simply put an instance of the value-based class is final, immutable and these instances do not have a proper state and identity, due to which certain operations are identity-specific and should be disallowed.

Java is gradually moving towards a functional style of programming and Value-Based classes help to promote immutability which is an integral aspect of the functional style of programming.

Java Optional Class Usage Guidelines

Java Optional is just thought to be used in the context of replacement for NullPointerException and since its introduction, there are various scenarios for which this is recommended, however when we see it through the lens of value-based class, we get the limitation and purpose of using Optional:

  1. Optional should not be used for equality check or identity-based operations. The following should not be done:
  2. It should not be used as a Key in a HashMap. Following should not be allowed:
  3. Optional should not be used as a non-transient variable in a Serializable class. The following should be avoided:
  4. Should not be used as an element of Entity, Data or Model class. These classes are used for persistence and hence would need identity and serialization; hence we should not use Optional instance variable.
  5. Java Optional class should not be used for synchronization. Following code should be avoided:
  6. Optional is primarily intended for use as a method return type where there is a clear need to represent “no result” and where using null is likely to cause errors.A variable whose type is Optional should never itself be null; it should always point to an Optional instance. Following way of coding is allowed:


In this article we discovered what a value-based class is? What are the scenarios for using a value-based class and the usage guidelines for Optional.

By admin

Leave a Reply

%d bloggers like this: