Java One Line if Statement

Rashmi Patidar Dec 21, 2022 Apr 08, 2021
  1. Ternary Operator in Java
  2. One Line if-else Statement Using filter in Java 8
Java One Line if Statement

There are 52 keywords or predefined words in the Java language. We call these words reserved as they have some specific predefined meaning in the language.

Out of this pool of reserved words, if-else is one of them. We use this keyword to specify any condition. The structure of if-else block looks like this:

if (condition == true) {
    doThis;
} else {
    doSomethingElse;
}

We can give any expression in the condition present inside parenthesis ().

If the expression in the if block results in true then, the doThis statement shall get executed. And if an expression evaluates to false, then doSomethingElse should be executed.

As we can see, it consumes five lines to do a simple if-else type of operation. The alternative to such kind of evaluations is to use ternary operators.

Ternary Operator in Java

A ternary operator is a short-hand form of Java if-else statement. The syntax of this operator is defined as below.

condition ? expression1 : expression2 ;

In the above statement, condition is first evaluated. If condition evaluates to true, then expression1 is executed. And if condition evaluates to false, then expression2 gets executed.

As the above operator takes three operands conditions and two expressions, so it is referred to as the ternary operator.

Below is the sample program to demonstrate the same.

package ternaryOperator;

public class TernaryOperator {
    public static void main(String[] args) {
        int marks = 67;
        String distinction = marks > 70 ? "Yes" : "No";
        System.out.println("Has made a distinction : " +distinction);
    }
}

In the above program, marks>70 is the if condition. ? is then clause and : is else part of it.

The program should evaluate whether the marks are more than some predefined number or not. As the condition that is marks > 70 gets evaluates to false, No gets printed over the console output.

Output for the above program is as below.

Has made a distinction: No

One Line if-else Statement Using filter in Java 8

Java 8 and higher versions have the utility of streams. The streams filter method takes a Predicate and behaves like if-else in Java language.

package streams;

import java.util.Arrays;
import java.util.List;

public class Java 8Streams {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("1", "2");
        stringList.stream()
                .filter(string -> string.equals("1"))
                .forEach(System.out::println);
    }
}

The above program instantiates a list using Arrays.asList() method. Here we have given 1 and 2 as the String values. Now we have made a stream of this list using the stream function. Once we create the stream, the filter function is applied. This function filters the data based on the condition that is defined. The -> operator is called the lambda operator. It iterates each value of the stream in the filter function. And if the condition is satisfied, the value goes to the forEach() method to perform final actions.

As there is no case defined to handle else condition, the value shall get simply bypass and will get dropped.

And the output of the above program is given below:

1
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Statement