Operators in Java
By
B.Prabadevi
SITE,VIT
Symbols on
which
operations are
performed
Operators in Java
Java Operators
• Operators in Java are the special type of tokens in Java which when
coupled with entities such as variables or constants or datatypes
result in a specific operation such as addition, multiplication or even
shifting of bits.
• Four major groups of java operators:
• Arithmetic
• Bitwise
• relational
• Logical
The Basic Arithmetic Operators
• Java Arithmetic Operators are used to perform arithmetic operations.
• There are mainly 5 Arithmetic Operators in Java
Operator Operation Result
+(binary) Addition Sum of two entities
+(unary) Unary Plus Returns the value of its operand
-(binary) Subraction Difference of two entities
-(unary) Unary minus Negates its single operand
* Multiplication Product of two entities
/ Division Quotient value of division
% Modulo Remainder after dividing the two operands.
The Basic Arithmetic Operators
• Guess the output:
Arithmetic Compound Assignment Operators
• Java provides special operators that can be used to combine an
arithmetic operation with an assignment.
Operator Usage Equivalent
+= a += 4; a=a+4
-= a -= 4; a=a-4
*= a*=4 a=a*4
= a=4 a=a4
%= a%=4 a=a%4
Guess the output
C=c+(a*b)=3+(6*8)=51
Increment and Decrement
• Prefix
• x = 42;
• y = ++x;
• Here y=43, because the increment occurs
before x is assigned to y
• Equivalent : x=x+1; y=x;
• Postfix
• x = 42;
• y = x++;
• Here y=42, because the value of x is obtained
before the increment operator is executed
• Equivalent: y = x; x = x + 1;
The ++ and the - - are Java’s increment and decrement operators.
Increment and Decrement
• Prefix
• x = 42;
• y = --x;
• Here y=41, because the decrement occurs
before x is assigned to y
• Equivalent : x=x-1; y=x;
• Postfix
• x = 42;
• y = x--;
• Here y=42, because the value of x is obtained
before the decrement operator is executed
• Equivalent: y = x; x = x - 1;
The ++ and the - - are Java’s increment and decrement operators.
Unary Operator
• Logical Not Operator in Java
• flips the value of a boolean value
• It is denoted by a !
The Bitwise Operators
• Java defines several (Binary Digit) 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.
• a = 5 = 0000 0101
• b = 7= 0000 0111
• a << 1 = 0000 1010 = 10
• a << 2 = 0001 0100 = 20
Operator Operation Usage (consider a=5
and b=7)
Result
~ Bitwise unary NOT ~a =(-a)-1 -6
& Bitwise AND a & b 5
| Bitwise OR a | b 7
^ Bitwise exclusive OR a ^ b 2
>> Shift right (signed) a >> b (a/(2^b)) 0
>>> Shift right zero fill (unsigned) a >>> b 1
<< Shift left a << b (a*(2^b)) 640
&= Bitwise AND assignment a &= b 5
|= Bitwise OR assignment a |= b 7
^= Bitwise exclusive OR assignment a ^= b 2
>>= Shift right assignment a >>= 2 1
>>>= Shift right zero fill assignment a >>>= 2 1
<<= Shift left assignment a <<= 2 20
Java uses two’s complement to store negative number
• 5 0101~a~5-a-1-5-1-6
Examples
• Let A=60
• A >> 2 will give 15 which is 1111
• A >>>2 will give 15 which is 0000 1111
• 40<<1 =40*(2^1) 40>>1=40/(2^1)
• -40<<1=-40*(2^1)=-80
Integer
division
What is 40>>5??
1
Unsigned (>>>) vs Signed (>>)
• >> shifts bits of the number towards the
right and also reserve sign bit, which is
leftmost bit.
• A sign bit represents the sign of a
number.
• If the sign bit is 0 then it represents a
positive number.
• If sign bit is 1, it represents a negative
number.
• The unsigned right shift operator always
fills the leftmost position with 0s because
the value is not signed.
• Since it always stores 0 in the sign bit, it is
also called zero fill right shift operator in
java.
• If you will apply the unsigned right shift
operator >>> on a positive number, it will
give the same result as that of >>.
• But in the case of negative numbers, the
result will be positive because the signed
bit is replaced by 0.
• 32-bit value that is actually being shifted
Both are division by 2
Unsigned (>>>) vs Signed (>>)
• Both signed right shift operator >> and unsigned right shift
operator >>> are used to shift bits towards right.
• The only difference between them is that >> preserve sign bits
whereas >>> does not preserve sign bit.
• It always fills 0 in the sign bit whether number is positive or negative.
Right shift operator
-10>>2
• Reserves the sign after
shifting
• -60>>2-3
• 6000111100
11000011
+1
----------------------------
-60 1111000100
11110001(-1)
11110000
00001111 -15
-1>>>24
• Useful and meaningful for 32- and 64-bit values.
• smaller values are automatically promoted to int in expressions
• sign-extension occurs and that the shift will take place on a 32-bit
rather than on an 8- or 16-bit value.
• +1 00000000 00000000 00000000 00000001
11111111 11111111 11111111 11111110 (+1)
-1 00000000 00000000 00000000 11111111 11111111
11111111 11111111
00000000 00000000 00000000 11111111255
-10>>>2
• 1000000000 00000000 00000000 00001010
• -1011111111 11111111 11111111 11110110
• >>>2
• 00111111 11111111 11111111 111111011,073741821
-10>>>21,073741821
0 0
Bitwise Operator Compound Assignments
• All of the binary bitwise operators have a compound form similar to
that of the algebraic operators, which combines the assignment with
the bitwise operation
Relational Operators
• The relational operators determine the relationship that one operand
has to the other
Operator Meaning
> Greater than
>= Greater than or
Equal to
< Less than
<= Less than or Equal to
== Equal to
!= Not Equal to
Priority Operator Meaning
1 >, >=, <, <= Greater than, Greater than or
Equal to, Less than, Less than
or Equal to
2 ==, != Equal to, Not Equal to
Comparison Operators with boolean and
numbers
• > has more priority than ==.
• So, a is compared with b first. In the end, the result of the first
relational expression is compared with false.
Boolean Logical Operators
• The Boolean logical operators shown here operate only on boolean
operands
Priority Operator Simple Name
1 ! Logical Unary NOT
2 & Logical AND
3 ^ Exclusive OR or XOR
4 | Logical OR
5 && Logical Short Circuit AND
6 || Logical Short Circuit OR
7 op= Compound Assignment
Operator
&=, |=, ^=
7 = Assignment
Except for Logical NOT(!), all other
logical operators have less priority
than Arithmetic and Relational
operators.
Truth table of Boolean Logical operators
• Guess the output
Short-Circuit Logical Operators
• These are secondary versions of the Boolean AND and OR operators,
and are commonly known as short-circuit logical operators
• the OR operator (||) results in true when A is true, no matter what B is.
• AND operator results in false when A is false, no matter what B is.
• Whereas & and | considers both operand values
• Short-circuit logical evaluation
• Java will not bother to evaluate the right-hand operand when the
outcome of the expression can be determined by the left operand
alone.
Example
Here b was decremented twice
but the short circuit operator
omits the other operand
The ?: Operator (Ternary Operator)
• Java includes a special ternary (three-way) operator that can replace
certain types of if then- else statements
• Here, condition can be any expression that evaluates to a boolean
value. If condition is true, then true part expression is evaluated;
otherwise, false part expression is evaluated.
• Both true part expression and false part expression are required to return the
same (or compatible) type, which can’t be void
Guess K??
Java instanceof
• The java instanceof operator is used to test whether the object is an
instance of the specified type (class or subclass or interface).
False
Operators Precedence and Associativity
• When an expression is evaluated,
there may be more than one
operators involved in an
expression.
• When more than one operator has
to be evaluated in an expression ,
Java interpreter has to decide
which operator should be
evaluated first.
• Java makes this decision on the
basis of the precedence and the
associativity of the operators.
• What if all operators in an
expression have same
priority?
• In that case the second property
associated with an operator comes
into play, which is associativity.
• Associativity tells the direction of
execution of operators that can be
either left to right or right to left.
Operators Precedence and Priority
• Arithmetic Operators
Operators Precedence and Priority
• Relational Operators
Operators Precedence and Priority
• Relational Operators
8
Operators Precedence and Priority
• Bitwise operators
Priority Operator (s) Associativity
1 [ ], ( ), DOT (.) Left to Right
2 ++, -- Postfix Right to Left
3 ++, -- (Prefix), ~, !, Unary Minus (-), Unary
Plus(+), (typecast L2R)
Right to Left
4 *, /, % Arithmetic Left to Right
5 +, - Arithmetic Left to Right
6 <<, >>, >>> Bitwise Left to Right
7 >, >=, <, <= Relational Left to Right
8 ==, != Relational Left to Right
9 & Bitwise, Logical Left to Right
10 ^ Bitwise, Logical Left to Right
11 | Bitwise, Logical Left to Right
12 && Bitwise, Logical Left to Right
13 || Bitwise, Logical Left to Right
14 ?: Ternary Operator Right to Left
15 -> Arrow Operator Left to Right
15 =, op= assignment, Compound Right to Left

Programming in Java-Operators in Java with examples

  • 1.
    Operators in Java By B.Prabadevi SITE,VIT Symbolson which operations are performed
  • 2.
  • 3.
    Java Operators • Operatorsin Java are the special type of tokens in Java which when coupled with entities such as variables or constants or datatypes result in a specific operation such as addition, multiplication or even shifting of bits. • Four major groups of java operators: • Arithmetic • Bitwise • relational • Logical
  • 4.
    The Basic ArithmeticOperators • Java Arithmetic Operators are used to perform arithmetic operations. • There are mainly 5 Arithmetic Operators in Java Operator Operation Result +(binary) Addition Sum of two entities +(unary) Unary Plus Returns the value of its operand -(binary) Subraction Difference of two entities -(unary) Unary minus Negates its single operand * Multiplication Product of two entities / Division Quotient value of division % Modulo Remainder after dividing the two operands.
  • 5.
    The Basic ArithmeticOperators • Guess the output:
  • 6.
    Arithmetic Compound AssignmentOperators • Java provides special operators that can be used to combine an arithmetic operation with an assignment. Operator Usage Equivalent += a += 4; a=a+4 -= a -= 4; a=a-4 *= a*=4 a=a*4 = a=4 a=a4 %= a%=4 a=a%4 Guess the output C=c+(a*b)=3+(6*8)=51
  • 7.
    Increment and Decrement •Prefix • x = 42; • y = ++x; • Here y=43, because the increment occurs before x is assigned to y • Equivalent : x=x+1; y=x; • Postfix • x = 42; • y = x++; • Here y=42, because the value of x is obtained before the increment operator is executed • Equivalent: y = x; x = x + 1; The ++ and the - - are Java’s increment and decrement operators.
  • 8.
    Increment and Decrement •Prefix • x = 42; • y = --x; • Here y=41, because the decrement occurs before x is assigned to y • Equivalent : x=x-1; y=x; • Postfix • x = 42; • y = x--; • Here y=42, because the value of x is obtained before the decrement operator is executed • Equivalent: y = x; x = x - 1; The ++ and the - - are Java’s increment and decrement operators.
  • 9.
    Unary Operator • LogicalNot Operator in Java • flips the value of a boolean value • It is denoted by a !
  • 10.
    The Bitwise Operators •Java defines several (Binary Digit) 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.
  • 11.
    • a =5 = 0000 0101 • b = 7= 0000 0111 • a << 1 = 0000 1010 = 10 • a << 2 = 0001 0100 = 20 Operator Operation Usage (consider a=5 and b=7) Result ~ Bitwise unary NOT ~a =(-a)-1 -6 & Bitwise AND a & b 5 | Bitwise OR a | b 7 ^ Bitwise exclusive OR a ^ b 2 >> Shift right (signed) a >> b (a/(2^b)) 0 >>> Shift right zero fill (unsigned) a >>> b 1 << Shift left a << b (a*(2^b)) 640 &= Bitwise AND assignment a &= b 5 |= Bitwise OR assignment a |= b 7 ^= Bitwise exclusive OR assignment a ^= b 2 >>= Shift right assignment a >>= 2 1 >>>= Shift right zero fill assignment a >>>= 2 1 <<= Shift left assignment a <<= 2 20 Java uses two’s complement to store negative number
  • 12.
  • 13.
    Examples • Let A=60 •A >> 2 will give 15 which is 1111 • A >>>2 will give 15 which is 0000 1111 • 40<<1 =40*(2^1) 40>>1=40/(2^1) • -40<<1=-40*(2^1)=-80 Integer division What is 40>>5?? 1
  • 14.
    Unsigned (>>>) vsSigned (>>) • >> shifts bits of the number towards the right and also reserve sign bit, which is leftmost bit. • A sign bit represents the sign of a number. • If the sign bit is 0 then it represents a positive number. • If sign bit is 1, it represents a negative number. • The unsigned right shift operator always fills the leftmost position with 0s because the value is not signed. • Since it always stores 0 in the sign bit, it is also called zero fill right shift operator in java. • If you will apply the unsigned right shift operator >>> on a positive number, it will give the same result as that of >>. • But in the case of negative numbers, the result will be positive because the signed bit is replaced by 0. • 32-bit value that is actually being shifted Both are division by 2
  • 15.
    Unsigned (>>>) vsSigned (>>) • Both signed right shift operator >> and unsigned right shift operator >>> are used to shift bits towards right. • The only difference between them is that >> preserve sign bits whereas >>> does not preserve sign bit. • It always fills 0 in the sign bit whether number is positive or negative.
  • 16.
  • 17.
    -10>>2 • Reserves thesign after shifting • -60>>2-3 • 6000111100 11000011 +1 ---------------------------- -60 1111000100 11110001(-1) 11110000 00001111 -15
  • 18.
    -1>>>24 • Useful andmeaningful for 32- and 64-bit values. • smaller values are automatically promoted to int in expressions • sign-extension occurs and that the shift will take place on a 32-bit rather than on an 8- or 16-bit value. • +1 00000000 00000000 00000000 00000001 11111111 11111111 11111111 11111110 (+1) -1 00000000 00000000 00000000 11111111 11111111 11111111 11111111 00000000 00000000 00000000 11111111255
  • 19.
    -10>>>2 • 1000000000 0000000000000000 00001010 • -1011111111 11111111 11111111 11110110 • >>>2 • 00111111 11111111 11111111 111111011,073741821 -10>>>21,073741821 0 0
  • 20.
    Bitwise Operator CompoundAssignments • All of the binary bitwise operators have a compound form similar to that of the algebraic operators, which combines the assignment with the bitwise operation
  • 21.
    Relational Operators • Therelational operators determine the relationship that one operand has to the other Operator Meaning > Greater than >= Greater than or Equal to < Less than <= Less than or Equal to == Equal to != Not Equal to Priority Operator Meaning 1 >, >=, <, <= Greater than, Greater than or Equal to, Less than, Less than or Equal to 2 ==, != Equal to, Not Equal to
  • 22.
    Comparison Operators withboolean and numbers • > has more priority than ==. • So, a is compared with b first. In the end, the result of the first relational expression is compared with false.
  • 23.
    Boolean Logical Operators •The Boolean logical operators shown here operate only on boolean operands Priority Operator Simple Name 1 ! Logical Unary NOT 2 & Logical AND 3 ^ Exclusive OR or XOR 4 | Logical OR 5 && Logical Short Circuit AND 6 || Logical Short Circuit OR 7 op= Compound Assignment Operator &=, |=, ^= 7 = Assignment Except for Logical NOT(!), all other logical operators have less priority than Arithmetic and Relational operators.
  • 24.
    Truth table ofBoolean Logical operators • Guess the output
  • 25.
    Short-Circuit Logical Operators •These are secondary versions of the Boolean AND and OR operators, and are commonly known as short-circuit logical operators • the OR operator (||) results in true when A is true, no matter what B is. • AND operator results in false when A is false, no matter what B is. • Whereas & and | considers both operand values • Short-circuit logical evaluation • Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left operand alone.
  • 26.
    Example Here b wasdecremented twice but the short circuit operator omits the other operand
  • 27.
    The ?: Operator(Ternary Operator) • Java includes a special ternary (three-way) operator that can replace certain types of if then- else statements • Here, condition can be any expression that evaluates to a boolean value. If condition is true, then true part expression is evaluated; otherwise, false part expression is evaluated. • Both true part expression and false part expression are required to return the same (or compatible) type, which can’t be void Guess K??
  • 28.
    Java instanceof • Thejava instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface). False
  • 29.
    Operators Precedence andAssociativity • When an expression is evaluated, there may be more than one operators involved in an expression. • When more than one operator has to be evaluated in an expression , Java interpreter has to decide which operator should be evaluated first. • Java makes this decision on the basis of the precedence and the associativity of the operators. • What if all operators in an expression have same priority? • In that case the second property associated with an operator comes into play, which is associativity. • Associativity tells the direction of execution of operators that can be either left to right or right to left.
  • 31.
    Operators Precedence andPriority • Arithmetic Operators
  • 32.
    Operators Precedence andPriority • Relational Operators
  • 33.
    Operators Precedence andPriority • Relational Operators 8
  • 34.
    Operators Precedence andPriority • Bitwise operators
  • 35.
    Priority Operator (s)Associativity 1 [ ], ( ), DOT (.) Left to Right 2 ++, -- Postfix Right to Left 3 ++, -- (Prefix), ~, !, Unary Minus (-), Unary Plus(+), (typecast L2R) Right to Left 4 *, /, % Arithmetic Left to Right 5 +, - Arithmetic Left to Right 6 <<, >>, >>> Bitwise Left to Right 7 >, >=, <, <= Relational Left to Right 8 ==, != Relational Left to Right 9 & Bitwise, Logical Left to Right 10 ^ Bitwise, Logical Left to Right 11 | Bitwise, Logical Left to Right 12 && Bitwise, Logical Left to Right 13 || Bitwise, Logical Left to Right 14 ?: Ternary Operator Right to Left 15 -> Arrow Operator Left to Right 15 =, op= assignment, Compound Right to Left