You are on page 1of 22

Operators and Expressions

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.

Operator Importance/ significance


+ Addition
- Subtraction
/ Division
* Multiplication
% Modulo division or remainder
Arithmetic operators
class OperatorExample{  
public static void main(String args[])
{  
int a=10;  
int b=5;  
System.out.println(a+b);   15
System.out.println(a-b);   5
System.out.println(a*b);   50
System.out.println(a/b);   2
System.out.println(a%b); 0
}}  
Relational Operators
• When evaluation of two numbers is performed depending upon their
relation, assured decisions are made.
• The value of relational expression is either true or false.
• If A=7 and A < 10 is true while 10 < A is false.
• Following table shows the details of operators.

Operator Importance/ significance


> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
== Is equal to
Relational Operators
public class Test {
public static void main(String args[]) {
int a = 10;
int b = 20;
System.out.println("a = = b = " + (a = = b) ); a == b = false
System.out.println("a != b = " + (a != b) ); a != b = true
System.out.println("a > b = " + (a > b) ); a > b = false
System.out.println("a < b = " + (a < b) ); a < b = true
System.out.println("b >= a = " + (b >= a) ); b >= a = true
System.out.println("b <= a = " + (b <= a) ); b <= a = false
}}
Logical operators
• When we want to form compound conditions by combining
two or more relations, then we can use logical operators.

Following table shows the details of operators.


Operators Importance/ significance
|| Logical – OR
&& Logical –AND
! Logical –NOT

op1 && op2

The logical expression defer a value of true or false


Logical operators
public class Test {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
} a && b = false
a || b = true
!(a && b) = true
Assignment Operator
•Assignment Operators is used to assign the value of an
expression to a variable and is also called as Shorthand
operators.
Variable_name binary_operator = expression;
•Following table show the use of assignment operators.
Simple Assignment Operator Statement with shorthand Operators
A=A+1 A+=1
A=A-1 A-=1
A=A/(B+1) A/=(B+1)
A=A*(B+1) A*=(B+1)
A=A/C A/=C
A=A%C A%=C
Assignment Operator
class OperatorExample{  
public static void main(String[] args){  
int a=10;  
a+=3;  
System.out.println(a);   13
a-=4;  
System.out.println(a);   9
a*=2;  
System.out.println(a);   18
a/=2;  
System.out.println(a);  }} 9
Increment and Decrement
Operators
• The increment operator ++ adds 1 to a variable.
• Usually the variable is an integer type, but it can be a floating point
type.

Expression Process Example end result


A++ Add 1 to a variable int A=10,B; A=11 B=10
after use. B=A++;
++A Add 1 to a variable int A=10,B; A=11 B=11
before use. B=++A;
A-- Subtract 1 from a int A=10,B; A=9 B=10
variable after use. B=A--;
--A Subtract 1 from a int A=10,B; A=9 B=9
variable before use. B=--A;
Increment and Decrement
Operators
class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=10;  
System.out.println(a++ + ++a);   22
System.out.println(b++ + b++);  21
}}  
Increment and Decrement 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:

Expression1 ? Expression2 : Expression3


• The operator ? : works as follows:
• Expression1 is evaluated if it is true then Expression2 is evaluated
and becomes the value of the conditional expression.

• If Expression1 is false then Expression3 is evaluated and its value


becomes the conditional expression.

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

person instanceof student

• 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)

You might also like