Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

OPERATORS AND

EXPRESSIONS
EXPRESSIONS
• Expressions are statements that return value
• Expressions uses operator
• OPERATORS : UNARY OPERATOR,BINARY OPERATOR,TERNARY
OPERATOR
• UNARY OPERATOR : i++;
• TERNARY OPERATOR : condition ? expression1 : expression2;
Ex: String result = (marks > 40) ? “PASS” : “FAIL”;
OPERATORS
• java provides a rich set of operators to manipulate variables. We can divide
all the Java operators into the following groups −
• Arithmetic Operators
• Unary Operators
• Assignment Operators
• Shorthand Assignment Operators
• Relational Operators
• Bitwise Operators
• Logical Operators
• Misc Operators
The Arithmetic 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 −
Assume integer variable A holds 10 and variable B holds 20, then −

Operator Description Example

Adds values on either side of the operator.


+ (Addition) A + B will give 30

Subtracts right-hand operand from left-hand


- (Subtraction) operand. A - B will give -10

Multiplies values on either side of the


* (Multiplication) operator. A * B will give 200

Divides left-hand operand by right-hand


/ (Division) operand. B / A will give 2

Divides left-hand operand by right-hand


operand and returns remainder.
% (Modulus) B % A will give 0
UNARY OPERATOR
• +,++,-,--
• INT A = +10;
• INT A = -10;
• i=i+1 can be written as i++;
• i--;
• ++i is prefix form
• X = 10;
z=++x;
POSTFIX AND PREFIX
• Int x=0;
int y = 0;
y = x++; //Y=X AND THEN INCREMENT
S.O.P(“Postfix”);
S.O.P(“x is” + x);
S.O.P(“y is” + y);
y = ++x; // Y=INCREMENT AND THEN ASSIGN
S.O.P(“Prefix”);
S.O.P(“x is” + x);
S.O.P(“y is” + y);
ASSIGNMENT OPERATOR:
Var = Expression;

Operator Description Example

Simple assignment operator. Assigns values


from right side operands to left side
operand. C = A + B will assign
=
value of A + B into C
SHORTHAND ASSIGNMENT
OPERATOR :
Var1 operator = Var2;
Operator Description Example

Add AND assignment operator. It adds right operand to the left operand and
+= assign the result to left operand. C += A is equivalent to C = C + A

Subtract AND assignment operator. It subtracts right operand from the left
-= operand and assign the result to left operand. C -= A is equivalent to C = C – A

Multiply AND assignment operator. It multiplies right operand with the left
*= operand and assign the result to left operand. C *= A is equivalent to C = C * A

/= Divide AND assignment operator. It divides left operand with the right operand
and assign the result to left operand. C /= A is equivalent to C = C / A

Modulus AND assignment operator. It takes modulus using two operands and
%= assign the result to left operand. C %= A is equivalent to C = C % A

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2


>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
The Relational Operators
There are following relational operators supported by Java language.
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
Checks if the values of two operands are equal or not, if yes then
== (equal to) condition becomes true. (A == B) is not true.

Checks if the values of two operands are equal or not, if values are not
equal then condition becomes true.
!= (not equal to) (A != B) is true.

Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true.
> (greater than) (A > B) is not true.

Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
< (less than) (A < B) is true.

Checks if the value of left operand is greater than or equal to the value
of right operand, if yes then condition becomes true.
>= (greater than or equal to) (A >= B) is not true.

Checks if the value of left operand is less than or equal to the value of
right operand, if yes then condition becomes true.
<= (less than or equal to) (A <= B) is true.
The Logical Operators
The following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −

Operator Description Example


Called Logical AND operator. If both the
&& (logical
operands are non-zero, then the condition (A && B) is false
and)
becomes true.
Called Logical OR Operator. If any of the two
|| (logical or) operands are non-zero, then the condition (A || B) is true
becomes true.
Called Logical NOT Operator. Use to reverses
! (logical not) the logical state of its operand. If a condition is !(A && B) is true
true then Logical NOT operator will make false.
The Logical Operators
The Logical Operators

class Main {
public static void main(String[] args) {

// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Java Bitwise and Bit Shift Operators

• The Bitwise operators are used to perform manipulation of individual


bits of a number which is an essential aspect of any programming
language as ultimately everything comes down to 0 and 1.
Binary Arithmetic
• Everything’s in powers of two
• Turn 78 into:

128 64 32 16 8 4 2 1
0 1 0 0 1 1 1 0
64 8 4 2
Logical Operators (Bit Level)
& | ^ ~
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12

a 00000000000000000000000000001010 10
& b 00000000000000000000000000001100 12
AND a & b 00000000000000000000000000001000 8

a 00000000000000000000000000001010 10
| b 00000000000000000000000000001100 12
OR a | b 00000000000000000000000000001110 14

a 00000000000000000000000000001010 10
^ b 00000000000000000000000000001100 12
XOR a ^ b 00000000000000000000000000000110 6

~ a
~a
00000000000000000000000000001010
11111111111111111111111111110101
10
-11
NOT
• Types of Bitwise Operators in Java
• & (Binary AND Operator)
• The Binary & operators are very much similar to the logical &&
operators, the only difference being they work with two bits instead
of two expressions. The Binary AND operator returns the value 1 is
both the operands are equal to one, else they return 0.
And Shift Your Bits ‘Round and
‘Round
• Bitwise AND of 78 and 34
128 64 32 16 8 4 2 1
78 0 1 0 0 1 1 1 0
34 0 0 1 0 0 0 1 0
2 0 0 0 0 0 0 1 0
• | ( Binary OR Operator)
• The Binary OR operator is similar to the logical || operator. It works
on two bits instead of two expressions and returns 1 if either one of
its operands evaluates as 1. The result is 1 even if both the operands
evaluate to 1.
• ^ ( Binary XOR Operator)
• XOR stand for “exclusive OR”. This operator returns 1, if exactly one of
its operands evaluate to 1. The result is 0, if both the operands
evaluate to 1 or 0.
• ~( Binary Complement Operator)
• The one’s complement of the input value is returned by this operator.
In simpler terms, it inverses the bits i.e. it converts the 0’s to 1’s and
vice versa.
• >>( Signed Right Shift Operator):
• This operator shifts the number to the right. It fills 0 in the empty
spaces that are left as a result. The leftmost bit is dependent upon the
sign of the initial number. Similar to dividing a number with some
power of two.
• >>>( Unsigned Right Shift Operator):
• This operator shifts the number to the right. It fills 0 in the empty
spaces that are left as a result. The leftmost bit is set to 0.
• <<( Left Shift Operator):
• This operator shifts the number to the left. It fills 0 in the empty spaces
that are left as a result. Similar to multiplying a number with some
power of two.
• <<<( Unsigned Left Shift Operator):
• Java does not provide any such operator, unlike the unsigned right shift.
Shift Operators << >>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4

a 00000000000000000000000000000011 3
a << 2 00000000000000000000000000001100 12
<<
Left b 11111111111111111111111111111100 -4
b << 2 11111111111111111111111111110000 -16

a 00000000000000000000000000000011 3
a >> 2 00000000000000000000000000000000 0
>>
Right b 11111111111111111111111111111100 -4
b >> 2 11111111111111111111111111111111 -1
Shift Operator >>>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4

a 00000000000000000000000000000011 3
a >>> 2 00000000000000000000000000000000 0
>>>
Right 0 b 11111111111111111111111111111100 -4
b >>> 2 00111111111111111111111111111111 +big
MISC.OPERATOR
• Conditional Operator( ?: ) :
Ternary Operator
• condition ? expression1 : expression2;
Ex: String result = (marks > 40) ? “PASS” : “FAIL”;
INSTANCE OF OPERATOR
• (object reference variable)instanceof (class/interface type)
String name = “JAVA”;
Boolean result = name instanceof String;
• class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);//true
}
}
Misc. Operator
1. Semi Colon ;
2. Curly Brackets {}
3. Paranthesis ()
4. Square Brackets []
5. Comma ,
6. Single quote ‘ ‘
7. Double Quote “”
8. Dot Operator .
9. New Operator
EXPRESSIONS IN JAVA

• Expressions are valid combination of variables , literals and operators


1. Types of Expressions
2. Evaluation of Expressions
3. Type conversion in Expressions
TYPES OF EXPRESSION
1. ARITHMETIC EXPRESSION :
(A*B+C+4)-5.5
2. BOOLEAN EXPRESSION
int x = 10;
int y = 4;
(x < 10)
(y > 1)
(x < 10)&&(y > 1)
STRING EXPRESSIONS
1. System.out.println(“the value of a=“+a+ ”and b =“+b);
2. System.out.println(9+5+1+”JAVA”); 15JAVA
3. System.out.println(“JAVA”+9+5+1); JAVA951
4. System.out.println(9+(5+(1+”JAVA”))); 951JAVA
Evaluation of Expression
• VARIABLE = EXPRESSION;
• SUM = a+d+f;
TYPE CONVERSION OF EXPRESSION
• Java allows you to perform operation with different types of data used in one expression.
• Note that if a variable of type char, byte or short is used in an expression, its value is
automatically converted to int while evaluating that expression.

• Portion of java conversion rule is called integral promotion. The integral promotion is
only in effect during the evaluation of an expression. The variable memory size does not
become larger.

• Automatic integral promotions are applied the java complier converts all operands 'up' to
the type of the largest operand in an expression is called type promotion.
TYPE CONVERSION OF EXPRESSION
1. char c = ‘1’;
int I = 1;
float f = 1.1;
double d = 34.67;
f = c + i – f+d;
2. Short s1 = 10;
short s2 = 20;
int s3 = s1+s2;
s3 = (short)s3;
Java Operator Precedence

• Operator precedence determines the order in which the operators in


an expression are evaluated.
Ex : int myint = 12 – 4 * 2;
Associativity of Operators in Java

• If an expression has two operators with similar precedence, the


expression is evaluated according to its associativity (either left to
right, or right to left).
a = b = c;
MATHEMATICAL FUNCTIONS IN JAVA
Method Description

Math.abs() It will return the Absolute value


of the given value.
Math.max() It returns the Largest of two
values.
Math.min() It is used to return the Smallest
of two values.
Math.round() It is used to round of the decimal
numbers to the nearest value.
Math.sqrt() It is used to return the square
root of a number.
Math.cbrt() It is used to return the cube root
of a number.
Math.pow() It returns the value of first
argument raised to the power to
second argument.

You might also like