Java

&& and & Operators in Java

In Java, && is called the conditional-And operator. It is an example of a logical operator in Java. As another operator, & has two functions in Java. In one situation, it is called a logical-And operator. In the other situation, it is called the bitwise-AND operator. Each of these operators is a binary operator. This means that each has an operand on its left and on its right. The result of all that expression can be assigned to a variable. These operators work with primitive types, and so its class does not have to be imported by the programmer.

The truth table for AND is:

false AND false = false

false AND true = false

true AND false = false

true AND true = true

This article explains the use of these three operators, beginning with the logical-And operator, &.

Logical & Operator

A boolean value is either true or false. This operator should be used, when both operands are boolean values. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
boolean bl1 = false & false; System.out.println(bl1);
boolean bl2 = false & true; System.out.println(bl2);
boolean bl3 = true & false; System.out.println(bl3);
boolean bl4 = true & true; System.out.println(bl4);
        }
    }

In Java, a program is a programmer implemented class. The name of the program file is the class name. The class should have the main() method. For this program, the name of the class is, TheClass. In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Each line prints its result. The output is:

false

false

false

true

confirming that this is AND logic.

The conditional-And Operator, &&

The operator, && is an AND operator, and it is used in if-conditions and loop-conditions. Its left operand is a general purpose expression, and its right operand is also a general purpose expression. The following program illustrates its same use, outside the if or loop condition:

    public class TheClass {
        public static void main(String[] args) {
boolean bl1 = 1==0 && 1==0; System.out.println(bl1);
boolean bl2 = 1==0 && 1==1; System.out.println(bl2);
boolean bl3 = 1==1 && 1==0; System.out.println(bl3);
boolean bl4 = 1==1 && 1==1; System.out.println(bl4);
        }
    }

In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Note the left and right operands for each && operator. Each of these operands is an expression. Each of these expressions results in true or false. So, a true expression could have been substituted with the bare-word, true, and a false expression could have been substituted with the bare-word, false. The output is:

false

false

false

true

confirming that this is AND logic.

The above program is rewritten, where each line of interest, is an if-compound-statement:

    public class TheClass {
        public static void main(String[] args) {
            if (1==0 && 1==0) System.out.println(true); else System.out.println(false);
            if (1==0 && 1==1) System.out.println(true); else System.out.println(false);
            if (1==1 && 1==0) System.out.println(true); else System.out.println(false);
            if (1==1 && 1==1) System.out.println(true); else System.out.println(false);
        }
    }

In the main() method here, there are four lines. Each line corresponds to a line in the AND truth table. Note the left and right operands for each && operator. Each of these operands is an expression. Each of these expressions results in true or false. So, a true expression could have been substituted with the bare-word, true, and a false expression could have been substituted with the bare-word, false. The output is:

false

false

false

true

confirming that this is AND logic.

The Bitwise-AND Operator, &

The AND truth table with bits is:

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

& is the AND operator for bits, as well as it is the AND operator for boolean values.

Now 1111111100000000 in hexadecimal is 0xff00, and in decimal, it is 65280.

Also, 1111000011110000 in hexadecimal is 0xf0f0, and in decimal, it is 61680.

Also, 1111000000000000 in hexadecimal is 0xf000, and in decimal, it is 61440.

Decimal means base 10.

ANDing binary numbers bit-by-bit is called bitwise ANDing, and the operator for this is &. So

1111111100000000 & 1111000011110000 = 1111000000000000

The same thing applies to their corresponding hexadecimal numbers. That is:

0xff00 & 0xf0f0 = 0xf000

The same thing applies to their corresponding decimal numbers. That is:

65280 & 61680 = 61440

The bitwise operator is normally used with hexadecimal numbers or decimal numbers.

Bitwise AND (&) with Hexadecimal Numbers

The following program bitwises 0xff00 & 0xff00 to have 0xf000:

    public class TheClass {
        public static void main(String[] args) {
            int num1 = 0xff00;
            int num2 = 0xf0f0;
            int num3 = num1 & num2;
System.out.println(num3);
        }
    }

num1 and num2 are declared and initialized with hexadecimal numbers. The third statement does the bitwise AND, using &, for these hexadecimal numbers. The last statement prints out the result. The output is 61440, which is the decimal equivalent of the expected, 0xf000.

Bitwise AND (&) with Decimal Numbers

The following program bitwises 65280 & 61680 to have 61440 (all decimal numbers):

    public class TheClass {
        public static void main(String[] args) {
            int num1 = 65280;
            int num2 = 61680;
            int num3 = num1 & num2;
System.out.println(num3);
        }
    }

num1 and num2 are declared and initialized with decimal numbers. The third statement does the bitwise AND, using &, for these decimal numbers. The last statement prints out the result. The output is 61440, which is the decimal equivalent of binary 1111000000000000.

Conclusion

In Java, && is called the conditional-And operator. In Java, & is the logical-And operator and also the bitwise operator. These three operators are binary operators, in the sense that each has a left operand and a right operand. && is used when the left and right operands are expressions, where each results in true or false. Either of these expressions can actually be substituted with true or false & is used when handling boolean values: true or false. & must be used when bitwise AND is required.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.