Java U 1 Lec 4 Notes Marked PDF
Java U 1 Lec 4 Notes Marked PDF
Today’s Target
Operators
Arithmetic
Relational
Logical
Bitwise
Assignment
Conditional
Object Oriented Programming with Java
Operators
“An operator in Java is a symbol or keyword used to perform operations on variables and
values.”
Example:
int a = 10, b = 5;
int sum = a + b; // '+' is an operator that adds two numbers
System.out.println(sum);
Types of Operators
Arithmetic Operators
Unary Arithmetic Operators
Binary Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Bitwise Operators
Assignment Operators
Conditional Operator (Ternary)
Object Oriented Programming with Java
Operators
Arithmetic Operators
These operators are used to perform basic mathematical operations such as addition,
subtraction, multiplication, division, and modulus.
Example
Operator Description Result
[int a=5,b=3;]
+ Addition a+b 8
- Subtraction a–b 2
* Multiplication a*b 15
/ Division (quotient) a/b 1
% Modulus (remainder) a%b 2
Object Oriented Programming with Java
Operators
Arithmetic Operators
Unary Arithmetic Operators: These operate on a single operand.
Examples: + (unary plus), - (unary minus), ++ (increment), -- (decrement).
Operators
Relational Operators
These operators compare two values and return a boolean result (true or false). They are
commonly used in decision-making.
Examples: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or
equal to), <= (less than or equal to).
Example
Operator Description Result
[int a=5, b=3;]
== Equal to a == b FALSE
!= Not equal to a != b TRUE
> Greater than a>b TRUE
< Less than a<b FALSE
>= Greater than or equal to a >= b TRUE
<= Less than or equal to a <= b FALSE
Object Oriented Programming with Java
Operators
Logical Operators
These operators are used to combine multiple boolean expressions and return a boolean
result. They are mainly used in conditional statements.
Examples: && (logical AND – returns true if both conditions are true), || (logical OR –
returns true if at least one condition is true), ! (logical NOT – reverses the boolean value).
Example
Operator Description [int a=5, b=3;] Result
[int c=3, d=1;]
Logical AND (both conditions
&& (a > b) && (c > d) TRUE
must be true)
Logical OR (at least one
|| (a < b) || (c > d) TRUE
condition must be true)
Logical NOT (reverses boolean
! !(a > b) FALSE
value)
Object Oriented Programming with Java
Operators
Bitwise Operators
These operators perform operations at the binary (bit) level, modifying individual bits in
an integer.
Examples: & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise Complement),
<< (Left Shift), >> (Right Shift), and >>> (Unsigned Right Shift).
Example
Operator Description [int a=5, b=3;] Result
[int c=8, d=1;]
& Bitwise AND a & b (0101 & 0011) 1 (0001)
| Bitwise OR a | b (0101 | 0011) 7 (0111)
^ Bitwise XOR a ^ b (0101 ^ 0011) 6 (0110)
~ Bitwise Complement ~a (~0101) -6
<< Left shift (multiplies by 2) c << d 16
>> Right shift (divides by 2) c >> d 4
>>> Unsigned right shift -c >>> d 2147483644
Object Oriented Programming with Java
Operators
Assignment Operators
These operators assign values to variables and also provide shorthand expressions to
modify values.
Examples: = (simple assignment), += (addition with assignment), -= (subtraction with
assignment), *= (multiplication with assignment), /= (division with assignment), %= (modulus
with assignment).
Example
Operator Description Result
[int a;]
= Assign value a = 10; 10 (a = 10)
+= Add and assign a += 5; 15 (a = a + 5)
-= Subtract and assign a -= 3; 12 (a = a – 3)
*= Multiply and assign a *= 2; 24 (a = a * 2)
/= Divide and assign a /= 2; 12 (a = a / 2)
%= Modulus and assign a %= 2; 0 (a = a % 2)
Object Oriented Programming with Java
Operators
Conditional Operator
This is a shorthand operator for if-else statements, allowing conditional expressions to be
evaluated in a single line.
Example
Operator Description Result
[int a=5, b=4;]
?: Shorthand for if-else int min = (a < b) ? a : b; 4