M2L2_lyst7383
M2L2_lyst7383
to
INTERNSHIP STUDIO
Module 04 | Lesson 01
Operators
W W W. I N T E R N S H I P S T U D I O. C O M
Contents
Arithmetic Operators
Compound Assignment Operators
Bitwise Operators
Relational Operators
Boolean Logical Operators
The ? Operator
Operator Precedence
W W W. I N T E R N S H I P S T U D I O. C O M
Arithmetic Operators
• Arithmetic operators are used in
mathematical expressions in the same
way that they are used in algebra.
• The operands of the arithmetic
operators must be of a numeric type.
W W W. I N T E R N S H I P S T U D I O. C O M
Arithmetic Operators Example1
public class OperatorExample{ Output:
public static void main(String args[]){ 15
int a=10; 5
int b=5; 50
System.out.println(a+b); //15 2
System.out.println(a-b); //5 0
System.out.println(a*b); //50
System.out.println(a/b); //2
System.out.println(a%b); //0
}}
W W W. I N T E R N S H I P S T U D I O. C O M
The Modulus Operator
• The modulus operator, %, returns the remainder of a division operation. It can be applied to
floating-point types as well as integer types.
W W W. I N T E R N S H I P S T U D I O. C O M
Compound Assignment Operators
W W W. I N T E R N S H I P S T U D I O. C O M
Assignment Operators Example1
public class CompoundAssignmentOperator { Output:
public static void main(String args[]) { z = x + y = 38
int x = 25; int y = 13; int z = 0;
z += x = 63
z = x + y;
System.out.println("z = x + y = " + z ); z -= x = 38
z += x ; z *= x = 950
System.out.println("z += x = " + z ); z /= x = 0
z -= x ; z %= x = 5
System.out.println("z -= x = " + z );
z *= x ;
System.out.println("z *= x = " + z );
x = 34; z = 17;
z /= x ;
System.out.println("z /= x = " + z );
x = 12; z = 17;
z %= x ;
System.out.println("z %= x = " + z );
}}
W W W. I N T E R N S H I P S T U D I O. C O M
Unary Operator Example1
//Java Unary Operator Example: ++ and -- Output:
10
public class OperatorExample{ 12
public static void main(String args[]){ 12
int x=10; 10
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}}
W W W. I N T E R N S H I P S T U D I O. C O M
Unary Operator Example2
//Java Unary Operator Example 2: ++ and – Output:
22
public class OperatorExample{ 21
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a); //10+12=22
System.out.println(b++ + b++); //10+11=21
}}
W W W. I N T E R N S H I P S T U D I O. C O M
Unary Operator Example3
//Java Unary Operator Example: ~ and ! output:
-6
public class OperatorExample{ 9
public static void main(String args[]){ false
int a = 5; true
int b= -10;
boolean c=true;
boolean d=false;
System.out.println(~a); //-6
System.out.println(~b); //9
System.out.println(!c); //false
System.out.println(!d); //true
}}
W W W. I N T E R N S H I P S T U D I O. C O M
Bitwise Operators
W W W. I N T E R N S H I P S T U D I O. C O M
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation.
Assume if a = 60 and b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
a&b = 0000 1100
W W W. I N T E R N S H I P S T U D I O. C O M
Logical Operators
W W W. I N T E R N S H I P S T U D I O. C O M
Logical Operators
The Bitwise NOT The Bitwise OR
• Also called the bitwise complement, the unary • The OR operator, |, combines bits such that if
NOT operator, ~, inverts all of the bits of its either of the bits in the operands is a 1, then the
operand. resultant bit is a 1, as shown here:
• For example, the number 42, which has the 00101010 42
following bit pattern: 00101010 becomes | 00001111 15
11010101 after the NOT operator is applied. 00101111 47
The Bitwise XOR
The Bitwise AND The XOR operator, ^, combines bits such that if
• The AND operator, &, produces a 1 bit if both exactly one operand is 1, then the result is 1.
operands are also 1. A zero is produced in all Otherwise, the result is zero. The following example
other cases. Here is an example: shows the effect of the ^.
00101010 42 00101010 42
& 00001111 15 ^ 00001111 15
00001010 10 00100101 37
W W W. I N T E R N S H I P S T U D I O. C O M
Logical Operators Example1
class BitLogic {
output:
public static void main(String args[]) {
String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", a = 0011
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
b = 0110
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary a|b = 0111
int c = a | b; a&b = 0010
int d = a & b;
int e = a ^ b;
a^b = 0101 ~a&b|
int f = (~a & b) | (a & ~b); a&~b = 0101 ~a =
int g = ~a & 0x0f; 1100
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}}
W W W. I N T E R N S H I P S T U D I O. C O M
Relational Operators
W W W. I N T E R N S H I P S T U D I O. C O M
Relational Operators Example1
class RelationalOperators { Output:
public static void main(String[] args) { num1 > num2 is false
int num1 =1; num1 < num2 is true
int num2 = 2; num1 >= num2 is false
num1 <= num2 is true
System.out.println("num1 > num2 is " + (num1 > num2));
num1 == num2 is false
System.out.println("num1 < num2 is " + (num1 < num2)); num1 != num2 is true
System.out.println("num1 >= num2 is " + (num1 >= num2));
System.out.println("num1 <= num2 is " + (num1 <= num2));
System.out.println("num1 == num2 is " + (num1 == num2));
System.out.println("num1 != num2 is " + (num1 != num2));
}
}
W W W. I N T E R N S H I P S T U D I O. C O M
Boolean Logical Operators
W W W. I N T E R N S H I P S T U D I O. C O M
The ? Operator
expression1 ? expression2 : expression3 Example ratio = denom == 0 ? 0 : num / denom;
class Ternary { Output:
public static void main(String args[]) { Absolute value of 10 is 10
int i, k; Absolute value of -10 is 10
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i System.out.print("Absolute
value of ");
System.out.println(i + " is " + k); } }
W W W. I N T E R N S H I P S T U D I O. C O M
Operator Precedence
W W W. I N T E R N S H I P S T U D I O. C O M
S U M M A RY
You got this Next session
W W W. I N T E R N S H I P S T U D I O. C O M