Prefer to return empty Object instead of null


There are many cases in which we can return an empty object instead of null. This is usually preferable since it helps to eliminate one of the most common problem: NullPointerException

Following qualify as empty object :
  • Empty String
  • zero-length Array
  • empty collection

Examples for empty object:-
  • String str = "";
  • int[] values = new int[0];
  • List<Integer> list = new ArrayList<>();

Collections class contains several type-safe methods which return an empty collection

Collections.emptyList();
Collections.emptySet();
Collections.emptyMap();

by default, these methods are also immutable and serializable.

Functional programming with Java - Part 1

 Recently I was reviewing one PR raised by team memeber and going through one utitlity method and found out there are too many muatable vari...