tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
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  :  
com/bethecoder/tutorials/guava/base_tests/FirstNonNullTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.guava.base_tests;

import com.google.common.base.Objects;

public class FirstNonNullTest {

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

}
   

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


                    Print

   



Books Recommendation

 
 
bl  br