0% found this document useful (0 votes)
4 views

Operators in Java

Uploaded by

sainikithaputta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Operators in Java

Uploaded by

sainikithaputta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Operators in Java

Introduction to Java Operators


• Definition: Operators are symbols that
perform operations on variables and values.
• Importance: Essential for performing
calculations, making decisions, and
manipulating data in Java.
Types of Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Unary Operators
• Ternary Operator
Arithmetic Operators
• Used to perform basic mathematical operations.
• Operators: +, -, *, /, %
• Examples:
• int a = 10;
• int b = 5;
• int sum = a + b;// sum = 15
• int difference = a - b; // difference = 5
• int product = a * b; // product = 50
• int quotient = a / b; // quotient = 2
• int remainder = a % b; // remainder = 0
Relational Operators
• Used to compare two values.
• Operators: ==, !=, >, <, >=, <=
• Examples:int a = 10;
• int b = 5;
• boolean isEqual = (a == b); // isEqual = false
• boolean isNotEqual = (a != b); // isNotEqual = true
• boolean isGreater = (a > b); // isGreater = true
• boolean isLesser = (a < b); // isLesser = false
• boolean isGreaterOrEqual = (a >= b); // isGreaterOrEqual =
true
• boolean isLesserOrEqual = (a <= b); // isLesserOrEqual = false
Logical Operators
• Used to combine multiple boolean expressions.
• Operators: &&, ||, !
• Examples:
• boolean a = true;
• boolean b = false;
• boolean andResult = (a && b); // andResult = false
• boolean orResult = (a || b); // orResult = true
• boolean notResult = !a; // notResult = false
Bitwise Operators
• Used to perform operations on individual bits.
• Operators: &, |, ^, ~, <<, >>, >>>
• Examples:
• int a = 5; // 0101 in binary
• int b = 3; // 0011 in binary
• int andResult = a & b; // andResult = 1 (0001 in
binary)
• int orResult = a | b; // orResult = 7 (0111 in
binary)
• int xorResult = a ^ b; // xorResult = 6 (0110 in binary)
• int complement = ~a; // complement = -6 (inverts all
bits)
• int leftShift = a << 1; // leftShift = 10 (1010 in binary)
• int rightShift = a >> 1; // rightShift = 2 (0010 in binary)
• int unsignedRightShift = a >>> 1; // unsignedRightShift
= 2 (0010 in binary)
Assignment Operators
• Used to assign values to variables.
• Operators: =, +=, -=, *=, /=, %=
• Examples:
• int a = 10;
• a += 5; // a = 15
• a -= 3; // a = 12
• a *= 2; // a = 24
• a /= 4; // a = 6
• a %= 4; // a = 2
Unary Operators
• Used with a single operand.
• Operators: +, -, ++, --, !
• Examples:
• int a = 10;
• int b = +a; // b = 10
• int c = -a; // c = -10
• a++; // a = 11
• a--; // a = 10
• boolean d = true;
• boolean e = !d; // e = false
Ternary Operator
• A shorthand for the if-else statement.
• Operator: ? :
• Example:
• int a = 10;
• int b = 5;
• int max = (a > b) ? a : b; // max = 10
Conclusion
• Java operators are essential for performing
various operations on data.
• Understanding operators helps in writing
efficient and effective code.
• Practice using different operators to become
proficient in Java programming.

You might also like