Posts

Java 10 - Local Variable Type Inference

Java 10 - Local Variable Type Inference In one of my previous blog, I have already discussed about Type Inference. If you don’t have the idea about Type Inference then you can check my blog here . If we want to understand Type Inference in one line then we can say that It is the capability of the compiler to automatically detect the datatype of a variable at the compiler time. What is Local Variable type inference? Java 10 added new feature that allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else), and the type is inferred by the JDK. It will, then, be the job of the compiler to figure out the datatype of the variable. Until Java 9 , we had to define the type of the local variable. E.g.: String message = “Welcome to Waheedtechblog.com”; The above statement is right as that’s how things have been since the inception of java b...

Java 8 - Optional

Java 8 - Optional In this blog, I’ll discuss about Optional class which was introduced in JDK8 under java.util.package. Optional class is used to represent a value is present or absent. With Optional class, developers have to less worry about NullPointerException and can work on neat and clean code. It contains at most one value. Advantages: · No need to check for null · No more NullPointerException at runtime Public final class Optional<T> extends Object { } Let’s understand all its usage with the help of example 1. Optional.empty() To create an empty Optional object we simply need to write // How to create empty Optional              Optional<String> optional = Optional. empty ();              System. out .println( "1. Optional: " + optional );     Output:             1. Optional: Optiona...