Unit II-Java Operators
Operators are symbols that perform operations on
variables and values. For example, + is an operator used
for addition, while * is also an operator used for
multiplication.
Operators in Java can be classified into 5 types:
•Arithmetic Operators
•Assignment Operators
•Relational Operators
•Logical Operators
•Unary Operators
•Bitwise Operators
Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data.
thmetic Operators
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Java Arithmetic Operators
• Output:
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
Assignment Operators
class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// assign value using =
var = a;
System.out.println("var using =: " + var);
// assign value using =+
var += a;
System.out.println("var using +=: " + var);
// assign value using =*
var *= a;
System.out.println("var using *=: " + var);
}
}
Assignment Operators
Output:
var using =: 4
var using +=: 8
var using *=: 32
Relational Operators
• Relational operators are used to check the relationship between two
operands.
class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
Relational Operators
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
Logical Operators
• Check whether an expression is true or false
• Decision making
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
}
}
Unary Operators
• Used with only one operand
• E.g.
• ++ is a unary operator that increases the value of a
variable by 1. That is, ++5 will return 6
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
Unary Operators
• // increment operator
• result1 = ++a;
• System.out.println("After increment: " + result1);
• System.out.println("Value of b: " + b);
• // decrement operator
• result2 = --b;
• System.out.println("After decrement: " + result2);
• }
• }
Unary Operators
• Output:
Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
Bitwise and Shift Operators
• Bitwise OR
class Main {
public static void main(String[] args) {
int number1 = 12, number2 = 25, result;
// bitwise OR between 12 and 25
result = number1 | number2;
System.out.println(result); // prints 29
}
}
Bitwise and Shift Operators
• Bitwise AND Operator
• The bitwise AND & operator returns 1 if and only if both
the operands are 1. Otherwise, it returns 0.
• E.g.
2 = 00001100 (In Binary)
25 = 00011001 (In Binary)
// Bitwise AND Operation of 12 and 25
00001100
& 00011001
____________
00001000 = 8 (In Decimal)
Bitwise AND Operator
class Main {
public static void main(String[] args) {
int number1 = 12, number2 = 25, result;
// bitwise AND between 12 and 25
result = number1 & number2;
System.out.println(result); // prints 8
}
}
Bitwise XOR Operator
• The bitwise XOR ^ operator returns 1 if and only if one
of the operands is 1. However, if both the operands are
0 or if both are 1, then the result is 0.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
// Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
____________
00010101 = 21 (In Decimal)
Bitwise XOR Operator
class Main {
public static void main(String[] args) {
int number1 = 12, number2 = 25, result;
// bitwise XOR between 12 and 25
result = number1 ^ number2;
System.out.println(result); // prints 21
}
}
Bitwise Complement Operator
• The bitwise complement operator is a unary
operator (works with only one operand). It is
denoted by ~.
• It changes binary digits 1 to 0 and 0 to 1.
• 2's Complement
• In binary arithmetic, we can calculate the binary
negative of an integer using 2's complement.
• 1's complement changes 0 to 1 and 1 to 0. And, if
we add 1 to the result of the 1's complement, we
get the 2's complement of the original number
Bitwise Complement Operator
// compute the 2's complement of 36
36 = 00100100 (In Binary)
1's complement = 11011011
2's complement:
11011011
+ 1
_________
11011100
Bitwise Complement Operator
class Main {
public static void main(String[] args) {
int number = 35, result;
// bitwise complement of 35
result = ~number;
System.out.println(result); // prints -36
}
}
Java Shift Operators
• Java Left Shift Operator
• The left shift operator shifts all bits towards the
left by a certain number of specified bits. It is
denoted by <<.
Java Left Shift Operator
class Main {
public static void main(String[] args) {
int number = 2;
// 2 bit left shift operation
int result = number << 2;
System.out.println(result); // prints 8
}
}
Signed Right Shift Operator
• The signed right shift operator shifts all bits
towards the right by a certain number of
specified bits. It is denoted by >>
// right shift of 8
8 = 1000 (In Binary)
// perform 2 bit right shift
8 >> 2:
1000 >> 2 = 0010 (equivalent to 2)
Signed Right Shift Operator
class Main {
public static void main(String[] args) {
int number1 = 8;
int number2 = -8;
// 2 bit signed right shift
System.out.println(number1 >> 2); // prints 2
System.out.println(number2 >> 2); // prints -2
}
}
References
• Java The Complete Reference, Mc Graw Hill
Edition, Ninth Edition Herbert Schildt 2016

OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx

  • 1.
    Unit II-Java Operators Operatorsare symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication. Operators in Java can be classified into 5 types: •Arithmetic Operators •Assignment Operators •Relational Operators •Logical Operators •Unary Operators •Bitwise Operators
  • 2.
    Java Arithmetic Operators Arithmeticoperators are used to perform arithmetic operations on variables and data. thmetic Operators class Main { public static void main(String[] args) { // declare variables int a = 12, b = 5; // addition operator System.out.println("a + b = " + (a + b)); // subtraction operator System.out.println("a - b = " + (a - b)); // multiplication operator System.out.println("a * b = " + (a * b)); // division operator System.out.println("a / b = " + (a / b)); // modulo operator System.out.println("a % b = " + (a % b)); } }
  • 3.
    Java Arithmetic Operators •Output: a + b = 17 a - b = 7 a * b = 60 a / b = 2 a % b = 2
  • 4.
    Assignment Operators class Main{ public static void main(String[] args) { // create variables int a = 4; int var; // assign value using = var = a; System.out.println("var using =: " + var); // assign value using =+ var += a; System.out.println("var using +=: " + var); // assign value using =* var *= a; System.out.println("var using *=: " + var); } }
  • 5.
    Assignment Operators Output: var using=: 4 var using +=: 8 var using *=: 32
  • 6.
    Relational Operators • Relationaloperators are used to check the relationship between two operands. class Main { public static void main(String[] args) { // create variables int a = 7, b = 11; // value of a and b System.out.println("a is " + a + " and b is " + b); // == operator System.out.println(a == b); // false // != operator System.out.println(a != b); // true
  • 7.
    Relational Operators // >operator System.out.println(a > b); // false // < operator System.out.println(a < b); // true // >= operator System.out.println(a >= b); // false // <= operator System.out.println(a <= b); // true } }
  • 8.
    Logical Operators • Checkwhether an expression is true or false • Decision making 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 } }
  • 9.
    Unary Operators • Usedwith only one operand • E.g. • ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6 class Main { public static void main(String[] args) { // declare variables int a = 12, b = 12; int result1, result2; // original value System.out.println("Value of a: " + a);
  • 10.
    Unary Operators • //increment operator • result1 = ++a; • System.out.println("After increment: " + result1); • System.out.println("Value of b: " + b); • // decrement operator • result2 = --b; • System.out.println("After decrement: " + result2); • } • }
  • 11.
    Unary Operators • Output: Valueof a: 12 After increment: 13 Value of b: 12 After decrement: 11
  • 12.
    Bitwise and ShiftOperators • Bitwise OR class Main { public static void main(String[] args) { int number1 = 12, number2 = 25, result; // bitwise OR between 12 and 25 result = number1 | number2; System.out.println(result); // prints 29 } }
  • 13.
    Bitwise and ShiftOperators • Bitwise AND Operator • The bitwise AND & operator returns 1 if and only if both the operands are 1. Otherwise, it returns 0. • E.g. 2 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise AND Operation of 12 and 25 00001100 & 00011001 ____________ 00001000 = 8 (In Decimal)
  • 14.
    Bitwise AND Operator classMain { public static void main(String[] args) { int number1 = 12, number2 = 25, result; // bitwise AND between 12 and 25 result = number1 & number2; System.out.println(result); // prints 8 } }
  • 15.
    Bitwise XOR Operator •The bitwise XOR ^ operator returns 1 if and only if one of the operands is 1. However, if both the operands are 0 or if both are 1, then the result is 0. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) // Bitwise XOR Operation of 12 and 25 00001100 ^ 00011001 ____________ 00010101 = 21 (In Decimal)
  • 16.
    Bitwise XOR Operator classMain { public static void main(String[] args) { int number1 = 12, number2 = 25, result; // bitwise XOR between 12 and 25 result = number1 ^ number2; System.out.println(result); // prints 21 } }
  • 17.
    Bitwise Complement Operator •The bitwise complement operator is a unary operator (works with only one operand). It is denoted by ~. • It changes binary digits 1 to 0 and 0 to 1. • 2's Complement • In binary arithmetic, we can calculate the binary negative of an integer using 2's complement. • 1's complement changes 0 to 1 and 1 to 0. And, if we add 1 to the result of the 1's complement, we get the 2's complement of the original number
  • 18.
    Bitwise Complement Operator //compute the 2's complement of 36 36 = 00100100 (In Binary) 1's complement = 11011011 2's complement: 11011011 + 1 _________ 11011100
  • 19.
    Bitwise Complement Operator classMain { public static void main(String[] args) { int number = 35, result; // bitwise complement of 35 result = ~number; System.out.println(result); // prints -36 } }
  • 20.
    Java Shift Operators •Java Left Shift Operator • The left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted by <<.
  • 21.
    Java Left ShiftOperator class Main { public static void main(String[] args) { int number = 2; // 2 bit left shift operation int result = number << 2; System.out.println(result); // prints 8 } }
  • 22.
    Signed Right ShiftOperator • The signed right shift operator shifts all bits towards the right by a certain number of specified bits. It is denoted by >> // right shift of 8 8 = 1000 (In Binary) // perform 2 bit right shift 8 >> 2: 1000 >> 2 = 0010 (equivalent to 2)
  • 23.
    Signed Right ShiftOperator class Main { public static void main(String[] args) { int number1 = 8; int number2 = -8; // 2 bit signed right shift System.out.println(number1 >> 2); // prints 2 System.out.println(number2 >> 2); // prints -2 } }
  • 24.
    References • Java TheComplete Reference, Mc Graw Hill Edition, Ninth Edition Herbert Schildt 2016