Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

Sunday, April 12, 2020

Java 8 Optional orElseThrow() Example | Throw Exception in Optional in Java 8

1. introduction


In this tutorial, We'll learn how to throw an exception if the option is empty. Optional API orElseThrow() method returns value from Optional if present. Otherwise, it will throw the exception created by the Supplier.

2. Syntax


public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
                                    throws X extends Throwable


Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new, ArithmeticException::new

Thursday, August 22, 2019

Java Program to Convert Degree Celsius to Kelvin & Kelvin to Celsius

1. Overview


In this tutorial, We'll learn how to convert Degree Celsius to kelvin in Java programming language. First, we will learn the formula and temperature conversion programs in java.

Degree celsius and kelvin are part of temparature mesures.

The formula for conversion:

K = ( °C + 273.15 )

Where K is kelvin and °C is degree celsius

Example 1:

Celsius: 100
Kelvin: 373.15

Example 2:

Celsius: 120
Kelvin: 393.15