0% found this document useful (0 votes)
9 views7 pages

Java Operators

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Java Operators

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lecture Note on Operators in Java

Operators in Java are special symbols or keywords used to perform operations on variables and
values. Java provides a rich set of operators, categorized based on their functionality.

1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations on numeric operands.

Operator Description Example

+ Addition 5 + 3 = 8

- Subtraction 5 - 3 = 2

* Multiplication 5 * 3 = 15

/ Division 15 / 3 = 5

% Modulus (remainder) 7 % 3 = 1

++ Increment (adds 1) x = 5; x++; // x = 6

-- Decrement (subtracts 1) x = 5; x--; // x = 4

Example:

int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 1

2. Relational Operators
Relational operators compare two operands and return a boolean result ( true or false ).

Operator Description Example


== Equal to 5 == 3 // false

!= Not equal to 5 != 3 // true

> Greater than 5 > 3 // true

< Less than 5 < 3 // false

>= Greater than or equal to 5 >= 3 // true

<= Less than or equal to 5 <= 3 // false

Example:

int x = 10, y = 20;


System.out.println("x > y: " + (x > y)); // false
System.out.println("x == y: " + (x == y)); // false

3. Logical Operators
Logical operators operate on boolean operands to perform logical operations.

Operator Description Example

&& Logical AND true && false // false

` `

! Logical NOT !true // false

Short-Circuit Evaluation:

&& evaluates the second operand only if the first is true .


|| evaluates the second operand only if the first is false .

Example:
boolean a = true, b = false;
System.out.println("a && b: " + (a && b)); // false
System.out.println("a || b: " + (a || b)); // true
System.out.println("!a: " + (!a)); // false

4. Bitwise Operators
Bitwise operators perform operations on individual bits of integer operands.

Operator Description Example

& Bitwise AND 5 & 3 // 1 (0101 & 0011 =


0001)

` ` Bitwise OR

^ Bitwise XOR 5 ^ 3 // 6 (0101 ^ 0011 =


0110)

~ Bitwise NOT ~5 // -6 (inverts bits)

<< Left shift 5 << 1 // 10 (0101 << 1 =


1010)

>> Right shift 5 >> 1 // 2 (0101 >> 1 =


0010)

>>> Unsigned right 5 >>> 1 // 2


shift

Example:

int x = 5, y = 3;
System.out.println("x & y: " + (x & y)); // 1
System.out.println("x << 1: " + (x << 1)); // 10

5. Assignment Operators
Assignment operators assign values to variables, often combining with other operations.

Operator Description Example

= Assign x = 5

+= Add and assign x += 3 // x = x + 3

-= Subtract and assign x -= 3 // x = x - 3

*= Multiply and assign x *= 3 // x = x * 3

/= Divide and assign x /= 3 // x = x / 3

%= Modulus and assign x %= 3 // x = x % 3

&= Bitwise AND and assign x &= 3

` =` Bitwise OR and assign

Example:

int x = 10;
x += 5; // x = 15
System.out.println("x after +=: " + x);

6. Ternary Operator
The ternary operator ( ?: ) is a shorthand for an if-else statement.

Syntax:

variable = (condition) ? expression1 : expression2;

If condition is true , expression1 is evaluated; otherwise, expression2 is evaluated.

Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20

7. Instanceof Operator
The instanceof operator checks if an object is an instance of a specific class or interface.

Syntax:

object instanceof ClassName

Example:

String str = "Hello";


System.out.println(str instanceof String); // true
System.out.println(str instanceof Object); // true

8. Operator Precedence
Operators have a precedence order that determines the sequence of evaluation in expressions.
Parentheses () can override precedence.

Precedence Table (High to Low):

1. Postfix: x++ , x--


2. Unary: ++x , --x , +x , -x , ! , ~
3. Multiplicative: * , / , %
4. Additive: + , -
5. Shift: << , >> , >>>
6. Relational: < , > , <= , >= , instanceof
7. Equality: == , !=
8. Bitwise AND: &
9. Bitwise XOR: ^
10. Bitwise OR: |
11. Logical AND: &&
12. Logical OR: ||
13. Ternary: ?:
14. Assignment: = , += , -= , etc.

Example:

int result = 5 + 3 * 2; // 3 * 2 evaluated first, then + 5


System.out.println(result); // 11

Example Program Combining Operators

public class OperatorDemo {


public static void main(String[] args) {
int a = 10, b = 3;
// Arithmetic
System.out.println("a + b: " + (a + b)); // 13
System.out.println("a % b: " + (a % b)); // 1

// Relational
System.out.println("a > b: " + (a > b)); // true

// Logical
boolean x = true, y = false;
System.out.println("x && y: " + (x && y)); // false

// Bitwise
System.out.println("a & b: " + (a & b)); // 2

// Ternary
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 10

// Assignment
a += 5;
System.out.println("a after += 5: " + a); // 15
}
}
Output:

a + b: 13
a % b: 1
a > b: true
x && y: false
a & b: 2
Max: 10
a after += 5: 15

You might also like