Java introduction to boolean

One of the primitive data types in Java is boolean. You can create boolean with boolean keyword. This type of variables can only contain true or false as values. This data types are very useful when used with if, while statement which are (decision-making statement with boolean conditions) or when we need to use logical operators like && (and), || (or).

public class MainClassPrimitiveBoolean {
    public static void main(String[] args) {
        boolean myBooleanVar = true; // It can be true or false
        /**
         * Use boolean to check if something is true or false.
         * It can take part of "if" statements and also logical operations
         * like && (and), || (or)
         * examples:
         */
        boolean falseVar  = false;
        if(falseVar)
        {
            System.out.println("This condition will never be printed, it is always false");
        }

        boolean trueVar  = true;
        if(trueVar)
        {
            System.out.println("This condition will always be printed because trueVar is true");
        }

        if(falseVar && trueVar)
        {
            System.out.println("This condition will never be printed, because false && true = false");
        }

        if(falseVar || trueVar)
        {
            System.out.println("This condition will always be printed, because false || true = true");
        }
    }
}

Output:

This condition will always be printed because trueVar is true
This condition will always be printed, because false || true = true

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.