Operator Types
ETL LABS PVT LTD – JAVA PROGRAMMING 74
Operator Types
1 2 3
Arithmetic Operators Relational Operators Bitwise Operators
4 5 6
Assignment Operators Logical Operators Conditional (or ternary) Operators
ETL LABS PVT LTD – JAVA PROGRAMMING 75
Arithmetic Operators
Assume A = 10 and B = 20
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x–y
* Multiplication Multiplies two values x*y
/ Division Divides one value from another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
ETL LABS PVT LTD – JAVA PROGRAMMING 76
Relational Operators
Assume A = 10 and B = 20
Operator Name Description Example
Checks if the values of two operands are equal or not, if yes then
== (equal to) (A == B) is not true.
condition becomes true.
(not equal Checks if the values of two operands are equal or not, if values are
!= (A != B) is true.
to) not equal then condition becomes true.
(greater Checks if the value of left operand is greater than the value of right
> (A > B) is not true.
than) operand, if yes then condition becomes true.
ETL LABS PVT LTD – JAVA PROGRAMMING 77
Relational Operators
Assume A = 10 and B = 20
Operator Name Description Example
Checks if the value of left operand is less than the value of right
< (less than) (A < B) is true.
operand, if yes then condition becomes true.
(greater
Checks if the value of left operand is greater than or equal to the
>= than or (A >= B) is not true.
value of right operand, if yes then condition becomes true.
equal to)
(less than or Checks if the value of left operand is less than or equal to the value of
<= (A <= B) is true.
equal to) right operand, if yes then condition becomes true.
ETL LABS PVT LTD – JAVA PROGRAMMING 78
Bitwise Operators
Assume A = 60 and B = 13
Operator Description Example
Binary AND Operator copies a bit to the result if it exists in
& (bitwise and) (A & B) will give 12 which is 00001100
both operands.
| (bitwise or) Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 00111101
Binary XOR Operator copies the bit if it is set in one operand
^ (bitwise XOR) (A ^ B) will give 49 which is 00110001
but not both.
(~A ) will give -61 which is 11000011 in
~ (bitwise Binary Ones Complement Operator is unary and has the
2's complement form due to a signed
compliment) effect of 'flipping' bits.
binary number.
ETL LABS PVT LTD – JAVA PROGRAMMING 79
Bitwise Operators
Assume A = 60 and B = 13
Operator Description Example
Binary Left Shift Operator. The left operands value is moved left by the A << 2 will give 240 which is
<< (left shift)
number of bits specified by the right operand. 11110000
Binary Right Shift Operator. The left operands value is moved right by A >> 2 will give 15 which is
>> (right shift)
the number of bits specified by the right operand. 1111
Shift right zero fill operator. The left operands value is moved right by
>>> (zero fill right A >>>2 will give 15 which is
the number of bits specified by the right operand and shifted values
shift) 00001111
are filled up with zeros.
ETL LABS PVT LTD – JAVA PROGRAMMING 80
Logical Operators
Assume A holds true and B holds false
Operator Description Example
Called Logical AND operator. If both the operands are non-zero, then the
&& (logical and) (A && B) is false
condition becomes true.
Called Logical OR Operator. If any of the two operands are non-zero, then
|| (logical or) (A || B) is true
the condition becomes true.
Called Logical NOT Operator. Use to reverses the logical state of its
! (logical not) !(A && B) is true
operand. If a condition is true then Logical NOT operator will make false.
ETL LABS PVT LTD – JAVA PROGRAMMING 81
Assignment Operators
Operator Description Example
Simple assignment operator. Assigns values from right side operands to C = A + B will assign value of A
=
left side operand. + B into C
Add AND assignment operator. It adds right operand to the left operand C += A is equivalent to C = C +
+=
and assign the result to left operand. A
Subtract AND assignment operator. It subtracts right operand from the C -= A is equivalent to C = C –
-=
left operand and assign the result to left operand. A
Multiply AND assignment operator. It multiplies right operand with the C *= A is equivalent to C = C *
*=
left operand and assign the result to left operand. A
Divide AND assignment operator. It divides left operand with the right C /= A is equivalent to C = C /
/=
operand and assign the result to left operand. A
ETL LABS PVT LTD – JAVA PROGRAMMING 82
Assignment Operators
Operator Description Example
Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to C = C %
%=
operands and assign the result to left operand. A
C <<= 2 is same as C =
<<= Left shift AND assignment operator.
C << 2
C >>= 2 is same as C =
>>= Right shift AND assignment operator.
C >> 2
C &= 2 is same as C =
&= Bitwise AND assignment operator.
C&2
C ^= 2 is same as C =
^= bitwise exclusive OR and assignment operator.
C^2
|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
ETL LABS PVT LTD – JAVA PROGRAMMING 83
Conditional (or ternary)
Operators
This operator consists of three operands
and is used to evaluate Boolean
expressions. The goal of the operator is
4 to decide, which value should be
assigned to the variable.
Syntax
variable x = (expression) ? value if true :
value if false
ETL LABS PVT LTD – JAVA PROGRAMMING 84