Optionals JDK8

import java.util.Optional;
import org.testng.annotations.Test;

/**
* Using Optionals JDK8
* @author asharda
*
*/
public class OptionalTest {

/**
* Demo Using Optional.
*/
@Test
public void testOptional()
{
Integer value1=null;
Integer value2=new Integer(10);
Optional<Integer> a =Optional.ofNullable(value1);
Optional<Integer> b =Optional.of(value2);
System.out.println(“Adding 2 numbers “+sum(a,b));
System.out.println(“Substracting 2 numbers “+substract(a,b));
}

/**
* Add 2 Numbers.
* @param num1
* @param num2
* @return
*/
public int sum(Optional<Integer> num1,Optional <Integer> num2)
{
int a=num1.orElse(0);
int b=num2.get();
return a+b;
}

/**
* Substract 2 numbers.
* @param num1
* @param num2
* @return
*/
public int substract(Optional<Integer> num1,Optional <Integer> num2)
{
int a=num1.orElse(0);
int b=num2.get();
return a-b;
}
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Java, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s