Professional Documents
Culture Documents
Operators
• An operator is symbols that specify operation to be performed
may be certain mathematical and logical operation.
• Categories of operators are as follows:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special Operators
Arithmetic operators
• Arithmetic operators are used to make mathematical expressions and
the working out as same in algebra.
• Java provides the fundamental arithmetic operators.
• These can operate on built in data type of Java.
• Following table shows the details of operators.
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++); 10
System.out.println(++x); 12
System.out.println(x--); 12
System.out.println(--x); 10
}}
class IncrementOpr
{
public static void main(String args[])
{
int m=10, n=20;
m=10
System.out.println(“m=” +m);
System.out.println(“n=”
n=20 +n);
System.out.println(“++m=” + ++m);
++m=11 ” +n++);
System.out.println(“n=
n++=20
System.out.println(“m=” +m);
System.out.println(“n=” +n);
} m=11
} n=21
Conditional Operators
• The character pair ?: is a ternary operator of Java, which is used to
construct conditional expressions of the following form:
A=3; B=4;
C=(A<B)?A:B; Answer C=3
C=(3<4)?3:4;
Conditional Operators
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Answer: 2
Bit Wise Operators
• These operators are used for testing the bits, or shifting
them to right or left.
• Following table shows bit wise operator
Operator Importance/ significance
| Bitwise OR
& Bitwise AND Example:
&= Bitwise AND assignment int a= 4;
|= Bitwise OR assignment int b = a<<3;
^ Bitwise Exclusive OR
<< Left shift
>> Right shift Output:
~ One‘s complement b =32
Bit Wise Operators
public class Test {
public static void main(String args[]) { c = a << 2; /* 240 = 1111 0000 */
int a = 60; /* 60 = 0011 1100 */ System.out.println("a << 2 = " +
int b = 13; /* 13 = 0000 1101 */ c );
c = a >> 2; /* 15 = 1111 */
int c = 0;
System.out.println("a >> 2 = " +
c = a & b; /* 12 = 0000 1100 */ c );
System.out.println("a & b = " + c ); c = a >>> 2; /* 15 = 0000 1111
c = a | b; /* 61 = 0011 1101 */ */
System.out.println("a | b = " + c ); System.out.println("a >>> 2 = " + c
);
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */
System.out.println("~a = " + c );
}
}
Special Operators
• Java supports some special operators of interest such as
instanceof operator and member selection operator (.) .
• Instanceof Operator :
• The instanceof is an object reference operator and returns true if the
object on the left hand is an instance of the class given on right-hand
side
• Dot Operator :
• The dot operator (.) is used to access the instance variable and
methods of class object.
Person1.age
Person1.salary()
Operator Precedence in Java:
• An arithmetic expression without any parentheses will be
calculated from left to right using the rules of precedence of
operators.
• There are two priority levels of arithmetic operators are as
follows:
• High priority (* / %)
• Low priority (+ -)
Operator Associativity Rank
[] Left to right 1 < Left to right
() Left to right
<= Left to right
. Left to right
- Right to left > Left to right 6
++ Right to left >= Left to right
2
-- Right to left Instanceof Left to right
! Right to left == Left to right
~ Right to left 7
!= Left to right
(type) Right to left
* Left to right & Left to right 8
/ Left to right 3 ^ Left to right 9
% Left to right | Left to right 10
+ Left to right && Left to right 11
4
- Left to right
|| Left to right 13
<< Left to right
>> Left to right ?: Right to left 13
5
>>> Left to right = Right to left 14
Mathematical Functions
• Mathematical Functions such as cos, sqrt, log, etc. are frequently
used.
• Java supports these functions through Math class defined in
java.lang package.
• These functions should be used as following :
Math.function_name();
Example : double y= Math.sqrt(x);
Function
Function sqrt()
sin() ceil()
cos() floor()
tan() round()
pow(x,y) abs(a)
exp(x) max(a, b)
log(x) min(a, b)