Java One Line if Statement

  1. Ternary Operator in Java
  2. One Line if-else Statement Using filter in Java8

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) {
} else {

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 Java8

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 Java8Streams {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("1", "2");
                .filter(string -> string.equals("1"))

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:

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.