tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Basic > First Non Null

First Non Null 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Objects.firstNonNull() API. It returns the first non null parameter of the given two parameters and throws NullPointerException if both parameters are null.

File Name  :  
Author  :  Sudhakar KV
Email  :  [email protected]
package com.bethecoder.tutorials.guava.base_tests;


public class FirstNonNullTest {

   @param args
  public static void main(String[] args) {
    String value = Objects.firstNonNull(System.getProperty("java.home")"C:\\Java\\jdk1.6.0");
    value = Objects.firstNonNull(System.getProperty("java.home2")"C:\\Java\\jdk1.6.0");
    try {
      value = Objects.firstNonNull(System.getProperty("java.home2")null);
    catch (NullPointerException e) {
      System.out.println("Both parameters are nulls");


It gives the following output,
C:\Program Files\Java\jdk1.6.0_10\jre
Both parameters are nulls


bl  br