If you are a software developer and java is your choice of programming language and you still do not use Guava libraries then you are surely missing a piece of cake.
The Google Guava Libraries are awesome set of utility classes which will ease your programming and improve your coding style and standard. I will come up with features and usage of libraries one by one.
Here I will start with some basic thing.


I will start with a question “What does `null` represent in java?”
The obvious answer I might get from you that “no value for this parameter”. However sometimes we return null to mean “error” or even a “false” or just “nothing”. To understand what is the exact use case of `null` we must have a little context of the program.

Some use case are also there where you don’t want nullable parameter. Finding bug for NullPointerException is trouble some. It will be nice if this null case can be taken care of during compilation only.

Take a case:

When Map.get() returns null, it can mean the value was absent, or the value was present and null; this is obviously ambiguous.

In a different use case

return object.getMethod(argument1, argument2);

We might forget that getMethod(argument1, argument2) can return null. Even argument1 or argument2 can be null while implementing getMethod.

Note: If I asked you to look up someone’s number and you said “there’s no number for that person” I’d say “what do you mean, they’re in there with an unlisted number or you just couldn’t find an entry for them at all?” Those two possible responses map nicely to Optional.absent() and null respectively. Optional.absent() is the definitive “positive negative”.

Another Usage

Optional<Integer> primes(Optional<Integer> from, int to);

Here the programmer clearly specifies that result might be absent and the method will work with from being absent where it might take a default value to start. Here we do not enforce that null cannot be a input parameter but we take care of that in Optional<T>

How to add Guava as a Maven dependency

Search Maven central for the version of Guava you want, and then paste the snippet into your pom.xml:


Methods of Optional

  1. Optional.absent()Returns an Optional Instance with no reference.
  2. Optional.fromNullable(T obj )Returns an Optional instance; if obj is null Optional.absent() is returned else Optional is returned with reference.
  3. Optional.of( obj )Returns an Optional instance containing non null reference of obj .
  4. T isPresent()
    Returns true if present else false.
  5. T get()
    returns the contained instance i.e. the object, which must be present.
  6. T orNull()This is inverse of fromNullable() this returns null if absent.
public Optional  <double> calculate(Optional<double > var1, Optional <double > var2) {
		if (var1.isPresent() && var1.get() > 1000) {
			return Optional.fromNullable(var1.get() * Optional.fromNullable(var2).or(0.2));
		return Optional.absent();

In the above snippet var1 and var2 are Optional instances. If var1 is present and is greater than 1000 then return an Optional instance containing product of var1 containing a double value and var2.


Note: If var2 is absent then we use 0.2 as default value.

Following snippet demonstrate orNull()

        possible = Optional.absent();



Hope this post will help you avoiding awful NPE’s and painful debugs.