Chapter – 4
Introduction
• Operators are the symbols which operates on value or a variable. It tells the
compiler to perform certain mathematical or logical manipulations.
• Can be of following categories:
• Unary – requires a single operand
• Binary – requires two operands
• Ternary – requires three operands
• Following are the types of operators:
• Arithmetic
• Relational
• Boolean Logical
• Bitwise
• Assignment
• Increment/Decrement
• Type comparison operator
Types of operators
Types of operators Description
Arithmetic operators (binary) These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus.
Assignment operators (binary) These are used to assign the values for the variables in programs.
Relational operators (binary) These operators are used to compare the value of two variables.
Boolean Logical operators (binary) These operators are used to perform logical operations on the given
two variables of Boolean data types.
Bitwise operators (binary) These operators are used to perform bit operations on given two
variables.
Conditional operators (ternary) Conditional operators return one value if condition is true and returns
another value is condition is false.
Increment/Decrement operators These operators are used to either increase or decrease the value of
(unary) the variable by one.
Type comparison operator (binary) instanceof. The operator checks whether the object is of a particular
type (class type or interface type).
Arithmetic Operators
Operator Description Example (A is 10, B is 20)
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A – B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B/A will give 2 and A/B will give 0.5(if float) and 0(if
int)
% Modulus Operator and remainder of after B % A will give 0 and A % B will give 10
an integer division
• General form is operand1 operator operand2
• The operands of the arithmetic operators must be of a numeric type.
• Cannot use them on boolean types, but you can use them on char types, since the char
type in Java is, essentially, a subset of int.
Compound Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from
C = A + B will assign value of A + B into C
right side operands to left side operand
+= Compound assignment operator, Add AND C += A is equivalent to C = C + A
assignment operator. It adds right operand to the
left operand and assign the result to left operand
-= Compound assignment operator, Subtract AND C -= A is equivalent to C = C - A
assignment operator. It subtract right operand from
the left operand and assign the result to left
operand, others would be “ *=, /=, %= “
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator, others would be C &= 2 is same as C = C & 2
“^=, |=“
• General form is v op= exp where v is a variable and op is a binary arithmetic operator.
This statement is equivalent to v = v op(exp)
Increment/Decrement Operators
• Increment operators are used to increase the value of the variable by one and decrement
operators are used to decrease the value of the variable by one in C programs.
• Syntax:
• Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Relational Operators
Operator Description Example (A is 10, B is 20)
== Checks if the values of two operands are equal or not, if yes then
A == B will give 0
condition becomes true.
!= Checks if the values of two operands are equal or not, if values are A != B will give 1
not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of right A > B will give 0
operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right A < B will give 1
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the A >= B will give 0
value of right operand, if yes then condition becomes true.
<=
Checks if the value of left operand is less than or equal to the value
A <= B will give 1
of right operand, if yes then condition becomes true.
• General form is operand1 relational-operator operand2 takes a value of true if the
condition is satisfied and false if condition is not satisfied.
Bitwise Operators
Operator Description Example (A is 60, B is 13)
& Bitwise AND Operator copies a bit to the result if it exists in both
(A & B) = 12 (0000 1100)
operands.
| Bitwise OR Operator copies a bit if it exists in either operand. (A | B) = 61 (0011 1101)
~ Bitwise NOT Operator is unary and has the effect of 'flipping' bits. (~A) = -61 (1100 0011 in 2’s complement
form)
^ Bitwise XOR Operator copies the bit if it is set in one operand but (A ^ B) = 49 (0011 0001)
not both.
<< Left Shift Operator. The left operand’s value is moved left by the A << 2 will give 240 which is 1111 0000
number of bits specified by the right operand.
>> Right Shift Operator. The left operand’s value is moved right by the A >> 2 will give 15 which is 0000 1111
number of bits specified by the right operand.
>>> Shift Right Zero Fill. The left operand’s value is moved right by the If A = -1, then A >>> 24 will give 255 whereas
number of bits specified by the right operand, filling in zero from A >> 24 will give -1 again.
the left side. The shift right zero fill operator looks at the integer to
the left of the operator as a 32-bit binary number.
Java defines several bitwise operators that can be applied to the integer types, long, int, short, char, and byte.
These operators act upon the individual bits of their operands.
Bitwise Operators
Operator Description
&= Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise Exclusive OR Assignment
>>= Shift Right Assignment
>>>= Shift Right Zero Fill Assignment
<<= Shift Left Assignment
Bitwise Operators
Bitwise Operators
Boolean Logical Operators
Operator Description Operator Description
& Logical AND &= AND Assignment
| Logical OR |= OR Assignment
^ Logical XOR (Exclusive OR) ^= XOR Assignment
|| Short-circuit OR == Equal To
&& Short-circuit AND != Not Equal To
! Logical unary NOT
?: Ternary if-then-else
The Boolean logical operators shown here operate
only on boolean operands.
All of the binary logical operators combine two
boolean values to form a resultant boolean value.
Boolean Logical Operators
Short-Circuit Logical Operators
• As you can see from the preceding table, the OR operator results in true when A is
true, no matter what B is.
• Similarly, the AND operator results in false when A is false, no matter what B is.
• If you use the || and && forms, rather than the | and & forms of these operators,
Java will not bother to evaluate the right-hand operand when the outcome of the
expression can be determined by the left operand alone.
• This is very useful when the right-hand operand depends on the value of the left
one in order to function properly.
Short-Circuit Logical Operators
• If (5 > 6 && 7 > 5) → false. Second statement will not be executed.
• If (7 > 5 && 5 < 6) → true. Second statement will be executed.
• If (7 > 5 || 5 > 6) → true. Second statement will not be executed.
• If (5 > 6 || 7 > 5) → false. Second statement will be executed.
• Difference in execution of
• if (denom != 0 && num / denom > 10) when denom = 0 and num = 30
• if (denom != 0 & num / denom > 10) when denom = 0 and num = 30
• if(c==1 & e++ < 100)
d = 100;
The ?: Operator
• A ternary operator pair “?:” is available to construct conditional expression of the
form
• Exp1 ? Exp2 : Exp3 where Exp1, Exp2 and Exp3 are expressions.
• The operator ?: works as follows: Exp1 is evaluated first. If it
is nonzero (true), then the expression Exp2 is evaluated and
becomes the value of the expression.
• If Exp1 is false, Exp3 is evaluated and its value becomes the
value of the expression.
• Syntax
• (Condition? true_value: false_value);
• E.g. a = 10;
b = 15;
x = a > b ? a : b
16
Type Comparison Operator
• This operator is used only for object reference variables. The operator checks whether the
object is of a particular type (class type or interface type).
• instanceof operator is written as: ( Object reference variable ) instanceof (class/interface type)
• If the object referred by the variable on the left side of the operator passes the IS-A check for
the class/interface type on the right side, then the result will be true. Following is the example:
• public classTest {
public static void main(String args[]){
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
• This would produce the following result: true
Type Comparison Operator
• public class Car {
public static void main(String args[]){
Car a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
• This would produce the following result: true
Precedence of Java Operators
Any Questions???