Thursday, 28 May 2015

Java Conditional Operators (or Short-Circuit Logical Operators)

The Java supports the following conditional operators:

|| (conditional OR)

&& (conditional AND)

The conditional operators && and || can be used to perform logical OR or AND operations on boolean operands. The operators are similar to boolean logical operators. The only difference is that if the result is definitely known after evaluating the first operand then second operand is not evaluated as discussed in the previous section. Hence these operators are also known as short-circuit logical operators. Sometimes this proves to be very useful in avoiding run-time exceptions.

For example, the following piece of code may result in a run-time exception, if the value of x is zero (assuming that x and y are integers), as it will lead to division by zero:

x = 0;

if( x != 0 & y/x > 5)

{

----

----

}

The possibility of the run-time exception can be avoided if we use the short-circuit operator && instead of boolean logical operator & as shown below:

x = 0;

if( x != 0 && y/x > 5)

{

----

----

}

Java Boolean Logical Operators

The Java supports the following boolean logical operators:

! (logical complement)

| (logical OR)

& (logical AND)

^ (logical exclusive OR)

Logical Complement ( ! ) Operator

This is a unary operator and the operand must always be a boolean expression. This acts as the negation operator, which negates a boolean expression as shown in the following table:

Boolean Expression(b) Logical Complement(!b)
true false
false true

Logical OR ( | ) Operator

The logical OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical OR operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 | b2)
false false false
false true true
true false true
true true true

If first boolean expression is true then value of the compound expression will be true irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) OR operator where the second boolean expression is not evaluated if the result of the first boolean expression is true.

If both the operands of the | operator are of integer type then this operator behaves like bit-wise OR operator.

Logical AND ( & ) Operator

The logical AND operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical AND operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 & b2)
false false false
false true false
true false false
true true true

If first boolean expression is false then value of the compound expression will be false irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) AND operator where the second boolean expression is not evaluated if the result of the first boolean expression is false.

If both the operands of the & operator are of integer type then this operator behaves like bit-wise AND operator.

Logical Exclusive OR or XOR ( ^ ) Operator

The logical exclusive OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical exclusive operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 ^ b2)
false false false
false true true
true false true
true true false

If both the operands of the ^ operator are of integer type then this operator behaves like bit-wise exclusive OR operator.

Boolean Logical Assignment Operators

The Java supports the following Boolean Logical Assignment Operators:

&=, |=, ^=

An assignment operator has the following syntax:

<variable><operator> = <expression>

The above assignment is equivalent to:

<variable> = <variable>  <operator> (<expression>)

For example the assignment:

 b &= b1 is equivalent to b = b & b1

Here b is a boolean variable and b1 is a boolean expression

Java Comparison Operators (Relational Operators)

The Java supports the following comparison operators:

<, <=, >, >=,  = =, != and instanceof operator

These operators can be classified into three categories:
  1. Ordinal Comparisons or Relational Operators
  2. Equality Comparisons or Equality Operators
  3. instanaceof  Operator

Ordinal Comparisons or Relational Operators

Java Supports the following ordinal comparisons/relational operators:

<, <=, >, >=

These operators are used to compare ordinal data types i.e. data types where values have numeric order.

These operators cannot be applied on boolean types and reference types as these types do not have any numeric ordering among the values.

Equality Comparisons or Equality Operators

Equality comparisons test whether two values are same. They can be applied on all Java types including boolean and reference data types.

There are certain restrictions when these operators are used to compare reference data types.

Only references of the objects, whose classes belong to common type hierarchy, can be compared.

instanaceof  Operator


The instanceof operator is used to test the class of an object. The instanceof operator has the

general form:

object   instanceof   type

Here, object is an instance of a class, and type is a class type. If object is an instance of the

specified type or instance of any sub-class of the specified type, then the instanceof operator returns true. Otherwise its result is false.

Java Increment and Decrement Operators

Java supports the increment operator ++ and the decrement operator . 

These operators behave like increment/decrement operators in C/C++.

Java Arithmetic Operators

Java has five arithmetic operators:

  1. + (Addition)
  2. - (Subtraction)
  3.  * (Multiplication)
  4. / (Division) and
  5. % (Modulus)

The operands can be integers, floating-points or both. The arithmetic operators can be applied on

all the primitive types except the boolean. If a binary operator is used to combine two operands

of similar type then the type of result will also be same. But if a binary operator is used to

combine operands of different types then operand of lower type gets converted to the higher type

before the evaluation and the type of the result will be same as that of operand of higher type.

The type conversion rules are discussed in a subsequent section.



The behavior of the +, -, *, and / operator is same as in C/C++. The modulus operator can be

applied to integer as well as floating-point types while in case of C/C++ it can be applied to

integer types only. For example the following is valid in Java:

6.4 % 2.1

The result of the above expression will be 0.1.


Arithmetic Assignment Operators

The Java supports the following Assignment Operators like C/C++:

+=, -=, *=, /=, %=

An assignment operator has the following syntax:

<variable><operator> = <expression>

The above assignment is equivalent to:

<variable> = <variable>  <operator> (<expression>)

For example the assignment:

 x += 5 is equivalent to x = x + 5

For example the assignment:

 x += a * c is equivalent to x = x + (a*c)