Day 3
Java Operators
Operator
An operator is a symbol that performs some operation.
Example:
int a = 10;
int b = 20;
int c = a + b;
In the above example:
• a and b are operands.
• + is an operator.
Types of Operators
1. Arithmetic operators
2. Relational/comparison operators
3. Logical operators
4. Increment & Decrement operators
5. Assignment operators
6. Ternary/conditional operator
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Works with only numeric data types.
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Relational/Comparison Operators
Comparison operators are used to compare two values (or variables).
It returns a boolean value (true/false).
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to X<=y
Logical Operators
Logical operators are used to perform logical AND, OR and NOT operations.
It returns a boolean value (true/false).
Operator Name Description Example
&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)
Truth table to demonstrate Logical Operators:
A B A && B A || B !A !B
True True True True False False
True False False True False True
False True False True True False
False False False False True True
Increment and decrement operators
Operator Name Description Example
++ Increment Increases the value of a variable by 1 ++x or x++
-- Decrement Decreases the value of a variable by 1 --x or x--
Pre-increment Vs Post-increment
The pre-increment (++x) and post-increment (x++) operators are used to increase the value of a
variable by 1. However, they differ in when the increment takes effect relative to other operations in
the expression.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
1. Pre-Increment (++x):
• The variable is incremented first, and then the updated value is used in the expression.
• Example: If x = 5, then ++x increments x to 6, and the value of the expression is 6.
2. Post-Increment (x++):
• The current value of the variable is used in the expression first, and then the variable is
incremented.
• Example: If x = 5, then x++ uses the value 5 in the expression, and after the expression is
evaluated, x is incremented to 6.
Pre-decrement Vs Post-decrement
The pre-decrement (--x) and post-decrement (x--) operators are used to decrease the value of a
variable by 1. The key difference between them is when the decrement operation takes effect
relative to other operations in the expression.
1. Pre-Decrement (--x):
• The variable is decremented first, and then the updated value is used in the expression.
• Example: If x = 5, then --x decrements x to 4, and the value of the expression is 4.
2. Post-Decrement (x--):
• The current value of the variable is used in the expression first, and then the variable is
decremented.
• Example: If x = 5, then x-- uses the value 5 in the expression, and after the expression is
evaluated, x is decremented to 4.
Java Assignment Operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
Difference between = and == operators:
= Operator (Assignment Operator):
• The = operator is used to assign a value to a variable.
• Example: int x = 5; assigns the value 5 to the variable x.
== Operator (Relational/comparison/equality Operator):
• The == operator is used to compare two values or expressions for equality.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
• Example: x == 5 checks if the value of x is equal to 5.
Ternary/conditional operator
The ternary operator in Java is a shorthand way of writing an if-else statement. It is also known as
the conditional operator.
Syntax:
condition? Output 1: Output2;
condition: A boolean expression that is evaluated to either true or false.
output1: The value returned if the condition is true.
output2: The value returned if the condition is false.
Categories of Operators
• Unary Operators (Works on single operand)
++ -- = += -= *= /= %= !
• Binary operators (Works on at least two operands)
+ - * / % > >= < <= != == && ||
• Ternary operator (Works on three operands)
?:
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Lab Assignments
Exercise 1: Create a program that defines two integer variables, a and b. Perform the following
operations and print the results:
1. Sum of a and b
2. Difference when b is subtracted from a
3. Product of a and b
4. Quotient when a is divided by b
5. Remainder when a is divided by b
Exercise 2: Write a program to swap the values of two variables, x and y, without using a third
variable.
Exercise 3: Create a program to calculate the area and perimeter of a rectangle. Use variables length
and width to store the dimensions.
Exercise 4: Write a program to convert a temperature from Celsius to Fahrenheit. Use a variable
Celsius to store the temperature.
Exercise 5: Create a program to calculate the simple interest. Use variables principal, rate, and time
to store the principal amount, interest rate, and time period, respectively.
Exercise 6: Create a program that calculates the average of three double values. Use variables num1,
num2, and num3 for the values.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan