Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators and Expressions

Similar presentations


Presentation on theme: "Operators and Expressions"— Presentation transcript:

1 Operators and Expressions

2 Operators Arithmetic operators Relational operators Logical operators
An operator is symbols that specify operation to be performed may be certain mathematical and logical operation. Categories of operators are as follows: Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special Operators

3 Importance/ significance
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

4 Arithmetic operators class OperatorExample{
public static void main(String args[]){   int a=10;   int b=5;   System.out.println(a+b);   System.out.println(a-b);   System.out.println(a*b);   System.out.println(a/b);   System.out.println(a%b); }}  

5 Importance/ significance
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

6 Relational Operators a == b = false a != b = true a > b = false
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

7 Importance/ significance
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. op1 && op2 The logical expression defer a value of true or false Operators Importance/ significance || Logical – OR && Logical –AND ! Logical –NOT

8 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

9 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

10 Assignment Operator class OperatorExample{ public static void main(String[] args){ int a=10; a+=3; System.out.println(a); a-=4; a*=2; a/=2; System.out.println(a); }} 13 9 18

11 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 after use. int A=10,B; B=A++; A=11 B=10 ++A Add 1 to a variable before use. B=++A; A=11 B=11 A-- Subtract 1 from a variable after use. B=A--; A=9 B=10 --A Subtract 1 from a variable before use. B=--A; A=9 B=9

12 Increment and Decrement Operators
class OperatorExample{ public static void main(String args[]){ int a=10; int b=10; System.out.println(a a); System.out.println(b++ + b++); }} 22 21

13 Increment and Decrement Operators
class OperatorExample{   public static void main(String args[]){   int x=10;   System.out.println(x++);   System.out.println(++x);  System.out.println(x--);   System.out.println(--x);   }}   10 12

14 class IncrementOpr { public static void main(String args[]) int m=10, n=20; System.out.println(“m=” +m); System.out.println(“n=” +n); System.out.println(“++m=” + ++m); System.out.println(“n= ” +n++); } m=10 n=20 ++m=11 n++=20 m=11 n=21

15 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

16 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

17 Importance/ significance
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 Example: int a= 4; int b = a<<3; Output: b =32

18 Bit Wise Operators public class Test { public static void main(String args[]) { int a = 60; /* 60 = */ int b = 13; /* 13 = */ int c = 0; c = a & b; /* 12 = */ System.out.println("a & b = " + c ); c = a | b; /* 61 = */ System.out.println("a | b = " + c ); c = a ^ b; /* 49 = */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = */ System.out.println("~a = " + c ); } c = a << 2; /* 240 = */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 15 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = */ System.out.println("a >>> 2 = " + c );

19 person instanceof student
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()

20 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 (+ -)

21 Operator Associativity Rank [ ] Left to right 1 ( ) 2 . - Right to left ++ -- ! ~ (type) * 3 / % + 4 << 5 >> >>> < Left to right 6 <= > >= Instanceof == 7 != & 8 ^ 9 | 10 && 11 || 13 ?: Right to left = 14

22 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 sqrt() ceil() floor() round() abs(a) max(a, b) min(a, b) Function sin() cos() tan() pow(x,y) exp(x) log(x)


Download ppt "Operators and Expressions"

Similar presentations


Ads by Google