Short circuit logic operators (&&, ||), Ternary operator (?) in Java

Short circuit logic operators: There are two short circuit logic operators in Java. One is && and the other is ||. They are extension to the regular & and | operators. These operators are used to skip the valuation of right hand operands and take in to consideration only the left hand operands.

For Example: if (denominator != 0 && numerator / denominator > 5)

By using && you make sure that there are no run time errors in the program even if denominator is 0.

However, if we write: if (denominator != 0 & numerator / denominator > 5)

There could be run time errors in the program, if denominator is 0.

Ternary operator?: ? is a ternary operator in Java, used to replace if, then, else, statements.


exp1 ? exp2 : exp3

Leave a Reply