Java Convert boolean to short

Tags: boolean short

In this Java core tutorial we learn how to convert a boolean value into short value in Java programming language.

How to convert boolean to short in Java

To convert an boolean value to short we simply map the boolean true value to 1 in short and false value to 0 in short.

short shortValue = (short) (booleanValue ? 1 : 0);

The following Java example code we convert a boolean true value to short.

BooleanToShortExample1.java

public class BooleanToShortExample1 {
    public static void main(String... args) {
        boolean booleanValue = true;

        short shortValue = (short) (booleanValue ? 1 : 0);

        System.out.println("Boolean value: " + booleanValue);
        System.out.println("Short value: " + shortValue);
    }
}
The output as below.
Boolean value: true
Short value: 1

In the following Java example code we convert a boolean false value to short.

BooleanToShortExample2.java

public class BooleanToShortExample2 {
    public static void main(String... args) {
        boolean booleanValue = false;

        short shortValue = (short) (booleanValue ? 1 : 0);

        System.out.println("Boolean value: " + booleanValue);
        System.out.println("Short value: " + shortValue);
    }
}
The output as below.
Boolean value: false
Short value: 0

Happy Coding 😊

Java Convert boolean to int

Java Convert boolean to byte

Java Convert boolean to long

Java Convert boolean to String