Professional Documents
Culture Documents
Operator's and Expressions
Operator's and Expressions
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 −
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
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 −
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
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
• 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