SlideShare a Scribd company logo
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

More Related Content

PPTX
Operators
PPTX
Computer programming 2 Lesson 7
PDF
itft-Operators in java
PDF
Java basic operators
PDF
Java basic operators
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
PPTX
Arithmetic Operators ____ java.pptx
DOCX
Operators
Operators
Computer programming 2 Lesson 7
itft-Operators in java
Java basic operators
Java basic operators
4_A1208223655_21789_2_2018_04. Operators.ppt
Arithmetic Operators ____ java.pptx
Operators

Similar to OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx (20)

PPTX
Operators in java
PPTX
Java Operators with Simple introduction.pptx
PPTX
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
PPTX
presentation on array java program operators
PPTX
Java Operators with Simple introduction.pptx
PPT
PPT
Java - Operators
PPTX
L3 operators
PPTX
L3 operators
PPTX
L3 operators
PPTX
Lecture-02-JAVA, data type, token, variables.pptx
PPT
object oriented programming java lectures
PPTX
Java chapter 3
PPTX
Opeartor &amp; expression
PPTX
Pj01 4-operators and control flow
PPTX
Operator in JAVA
PPTX
Operators in java By cheena
PDF
4.Lesson Plan - Java Operators.pdf...pdf
PDF
5_Operators.pdf
PPTX
Operators in java presentation
Operators in java
Java Operators with Simple introduction.pptx
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
presentation on array java program operators
Java Operators with Simple introduction.pptx
Java - Operators
L3 operators
L3 operators
L3 operators
Lecture-02-JAVA, data type, token, variables.pptx
object oriented programming java lectures
Java chapter 3
Opeartor &amp; expression
Pj01 4-operators and control flow
Operator in JAVA
Operators in java By cheena
4.Lesson Plan - Java Operators.pdf...pdf
5_Operators.pdf
Operators in java presentation
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
Digital Logic Computer Design lecture notes
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Sustainable Sites - Green Building Construction
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Well-logging-methods_new................
PPTX
Construction Project Organization Group 2.pptx
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT on Performance Review to get promotions
Digital Logic Computer Design lecture notes
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CH1 Production IntroductoryConcepts.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Sustainable Sites - Green Building Construction
Automation-in-Manufacturing-Chapter-Introduction.pdf
Well-logging-methods_new................
Construction Project Organization Group 2.pptx
web development for engineering and engineering
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
Foundation to blockchain - A guide to Blockchain Tech
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Ad

OOPJ_PPT2,JAVA OPERATORS TPYE WITH EXAMPLES.pptx

  • 1. 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
  • 2. 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)); } }
  • 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 • 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
  • 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 • 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 } }
  • 9. 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);
  • 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: Value of a: 12 After increment: 13 Value of b: 12 After decrement: 11
  • 12. 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 } }
  • 13. 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)
  • 14. 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 } }
  • 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 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 } }
  • 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 class Main { 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 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 } }
  • 22. 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)
  • 23. 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 } }
  • 24. References • Java The Complete Reference, Mc Graw Hill Edition, Ninth Edition Herbert Schildt 2016