Uploaded by 08.Rajvardhan Pilankar

operators-and-expressions

advertisement
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.
2.
3.
4.
5.
6.
7.
8.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
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) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}}
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
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=A-1
A=A/(B+1)
A=A*(B+1)
A=A/C
A=A%C
A+=1
A-=1
A/=(B+1)
A*=(B+1)
A/=C
A%=C
Assignment Operator
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;
13
System.out.println(a);
a-=4;
System.out.println(a);
9
a*=2;
18
System.out.println(a);
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
A++
++A
A---A
Process
Add 1 to a variable
after use.
Add 1 to a variable
before use.
Subtract 1 from a
variable after use.
Subtract 1 from a
variable before use.
Example
int A=10,B;
B=A++;
int A=10,B;
B=++A;
int A=10,B;
B=A--;
int A=10,B;
B=--A;
end result
A=11 B=10
A=11 B=11
A=9 B=10
A=9 B=9
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
10
System.out.println(--x);
}}
class IncrementOpr
{
public static void main(String args[])
{
int
m=10,
n=20;
m=10
System.out.println(“m=” +m);
n=20
System.out.println(“n=”
+n);
++m=11
System.out.println(“++m=”
+ ++m);
System.out.println(“n=
” +n++);
n++=20
System.out.println(“m=”
+m);
m=11
System.out.println(“n=” +n);
} 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;
C=(3<4)?3:4;
Answer C=3
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
Bitwise AND assignment
Bitwise OR assignment
Bitwise Exclusive OR
Left shift
Right shift
One‘s complement
Bit Wise Operators
public class Test {
public static void main(String args[])
{
int a = 60;/* 60 = 0011 1100 */
int b = 13;
/* 13 = 0000 1101 */
int c = 0;
c = a & b;
/* 12 = 0000 1100 */
System.out.println("a & b = " + c );
c = a | b;
/* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b;
/* 48 = 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
[]
()
.
++
-!
~
(type)
*
/
%
+
<<
>>
>>>
Associativity
Left to right
Left to right
Left to right
Right to left
Right to left
Right to left
Right to left
Right to left
Right to left
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Rank
1
2
3
4
5
<
<=
>
>=
Instanceof
==
!=
&
^
|
&&
||
?:
=
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Right to left
Right to left
6
7
8
9
10
11
13
13
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)
Download