Mod 1 Part2
Mod 1 Part2
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into
the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment
OperatorsMisc
Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in
algebra. The following table lists the arithmetic operators:
- Subtraction - Subtracts right hand operand from left A - B will give -10
handoperand
* Multiplication - Multiplies values on either side of the operator A * B will give 200
/ Division - Divides left hand operand by right hand operand B / A will give 2
% Modulus - Divides left hand operand by right hand operand B % A will give 0
andreturns remainder
< Checks if the value of left operand is less than the value of (A < B) is true.
rightoperand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not
the value of right operand, if yes then condition becomes true. true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
Java defines several bitwise operators, which can be applied to the integer types, long, int, short,char,
and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13;
nowin binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
~a = 1100 0011
Operator Description
<< Binary Left Shift Operator. The left operands value is moved
leftby the number of bits specified by the right operan
>> Binary Right Shift Operator. The left operands value is
moved right by the number of bits specified by the right
operand.
>>> Shift right zero fill operator. The left operands value is moved
right by the number of bits specified by the right operand
andshifted values are filled up with zeros.
&& Called Logical AND operator. If both the operands are non- (A && B) is
zero,then the condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state true.
ofits operand. If a condition is true then Logical NOT operator
will make false.
!(A && B) is true.
Operator Description
Misc Operators
Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three operands
and is used to evaluate Boolean expressions. The goal of the operator is to decide which value
should be assigned to the variable. The operator is written as:
Value of b is : 30
Value of b is : 20
Instance of 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 wriiten as:
If the object referred by the variable on the left side of the operator passes the IS-A check for the
class/int erface type on the right side, then the result will be t rue. Following is the example:
public class Test {
true
This operator will still return true if the object being compared is the assignment compatible with the
type on the right. Following is one more example:
class Vehicle {}
true
Precedence of Java Operators:
Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator:
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.