Corporate Training Study
Material
Operators &
Expressions in Java
Objective of This Session
By the end of this
session, participants will be able to:
✅ Understand different
types of operators in Java.
✅ Use arithmetic,
relational, logical, bitwise, assignment, and ternary operators
effectively.
✅ Implement expressions
in Java programs with practical examples.
✅ Recognize operator precedence
and associativity.
1. Introduction to
Operators in Java
Operators are special
symbols that perform operations on variables and values in Java. They
are used to manipulate data and execute expressions efficiently.
For example:
int a = 10, b = 5;
int sum = a + b; // '+' is an arithmetic operator
System.out.println("Sum:
" + sum);
Output:
Sum: 15
2. Types of Operators in
Java
Java provides various
types of operators, categorized as follows:
Operator Type |
Description |
Arithmetic Operators |
Perform mathematical
operations. |
Relational (Comparison)
Operators |
Compare values and
return boolean results. |
Logical Operators |
Perform logical
operations like AND, OR, NOT. |
Bitwise Operators |
Work on bits for binary
operations. |
Assignment Operators |
Assign values to
variables. |
Ternary Operator |
A shorthand for if-else
conditions. |
3. Arithmetic Operators
Arithmetic operators
perform basic mathematical operations such as addition, subtraction,
multiplication, and division.
Operator |
Description |
Example |
+ |
Addition |
a + b (5 + 2 = 7) |
- |
Subtraction |
a - b (5 - 2 = 3) |
* |
Multiplication |
a * b (5 * 2 = 10) |
/ |
Division |
a / b (5 / 2 = 2)
(integer division) |
% |
Modulus (Remainder) |
a % b (5 % 2 = 1) |
Example:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Addition:
" + (a + b));
System.out.println("Subtraction:
" + (a - b));
System.out.println("Multiplication:
" + (a * b));
System.out.println("Division:
" + (a / b)); // Integer division
System.out.println("Modulus:
" + (a % b)); // Remainder
}
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
4. Relational
(Comparison) Operators
These operators are used
to compare two values and return true or false.
Operator |
Description |
Example |
== |
Equal to |
a == b |
!= |
Not equal to |
a != b |
> |
Greater than |
a > b |
< |
Less than |
a < b |
>= |
Greater than or equal
to |
a >= b |
<= |
Less than or equal to |
a <= b |
Example:
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("a == b: "
+ (a == b));
System.out.println("a != b: "
+ (a != b));
System.out.println("a > b:
" + (a > b));
System.out.println("a < b:
" + (a < b));
}
}
Output:
a == b: false
a != b: true
a > b: false
a < b: true
5. Logical Operators
Logical operators are
used to perform logical operations on boolean values.
Operator |
Description |
Example |
&& |
Logical AND |
true && false →
false |
` |
` |
|
! |
Logical NOT |
!true → false |
Example:
public class LogicalExample {
public static void main(String[] args) {
boolean x = true, y = false;
System.out.println("x &&
y: " + (x && y));
System.out.println("x || y: "
+ (x || y));
System.out.println("!x: " +
(!x));
}
}
Output:
x && y: false
x || y: true
!x: false
6. Bitwise Operators
Bitwise operators perform
operations on binary numbers.
Operator |
Description |
& |
Bitwise AND |
` |
` |
^ |
Bitwise XOR |
~ |
Bitwise Complement |
<< |
Left Shift |
>> |
Right Shift |
Example:
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3; // 5 = 0101, 3 = 0011
System.out.println("a & b:
" + (a & b)); // 0001 -> 1
System.out.println("a | b: "
+ (a | b)); // 0111 -> 7
System.out.println("a ^ b: "
+ (a ^ b)); // 0110 -> 6
System.out.println("~a: " +
(~a)); // Inverts bits
}
}
7. Assignment Operators
Assignment operators are
used to assign values to variables.
Operator |
Example |
Equivalent To |
= |
a = 10 |
a = 10 |
+= |
a += 5 |
a = a + 5 |
-= |
a -= 5 |
a = a - 5 |
*= |
a *= 5 |
a = a * 5 |
/= |
a /= 5 |
a = a / 5 |
%= |
a %= 5 |
a = a % 5 |
8. Ternary Operator
The ternary operator is a
shortcut for if-else statements.
Syntax:
condition ? expression1 : expression2;
Example:
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int min = (a < b) ? a : b;
System.out.println("Smaller value
is: " + min);
}
}
Output:
Smaller value is: 10
9. Operator Precedence
& Associativity
Java follows a precedence
order to decide which operator executes first.
Operator |
Precedence (Highest to
Lowest) |
(), [], . |
Highest |
*, /, % |
Multiplication,
Division, Modulus |
+, - |
Addition, Subtraction |
<<, >>, >>> |
Bitwise Shift |
==, != |
Relational Operators |
&&, ` |
|
= |
Assignment (Lowest) |
Conclusion
✅ Java operators allow us to perform
various operations efficiently.
✅ Understanding operator
precedence helps in writing error-free expressions.
✅ Logical, bitwise, and
ternary operators help in efficient decision-making.
Assignments on Java Operators & Expressions
📌 Assignment 1: Arithmetic
Operators
Task:
Write a Java program to
demonstrate arithmetic operations.
Solution:
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 15, b = 4;
System.out.println("Addition:
" + (a + b));
System.out.println("Subtraction:
" + (a - b));
System.out.println("Multiplication:
" + (a * b));
System.out.println("Division:
" + (a / b)); // Integer division
System.out.println("Modulus:
" + (a % b)); // Remainder
}
}
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulus: 3
📌 Assignment 2: Relational
Operators
Task:
Compare two numbers using
relational operators.
Solution:
public class RelationalOperators {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: "
+ (x == y));
System.out.println("x != y: "
+ (x != y));
System.out.println("x > y:
" + (x > y));
System.out.println("x < y:
" + (x < y));
System.out.println("x >= y:
" + (x >= y));
System.out.println("x <= y:
" + (x <= y));
}
}
Output:
x == y: false
x != y: true
x > y: false
x < y: true
x >= y: false
x <= y: true
📌 Assignment 3: Logical
Operators
Task:
Use logical operators to
check conditions.
Solution:
public class LogicalOperators {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println("a &&
b: " + (a && b));
System.out.println("a || b: "
+ (a || b));
System.out.println("!a: " +
(!a));
}
}
Output:
a && b: false
a || b: true
!a: false
📌 Assignment 4: Bitwise
Operators
Task:
Perform bitwise
operations on two numbers.
Solution:
public class BitwiseOperators {
public static void main(String[] args) {
int x = 5, y = 3; // 5 = 0101, 3 = 0011
System.out.println("x & y:
" + (x & y)); // AND
System.out.println("x | y: "
+ (x | y)); // OR
System.out.println("x ^ y: "
+ (x ^ y)); // XOR
System.out.println("~x: " +
(~x)); // NOT
}
}
Output:
x & y: 1
x | y: 7
x ^ y: 6
~x: -6
📌 Assignment 5: Assignment
Operators
Task:
Use different assignment
operators.
Solution:
public class AssignmentOperators {
public static void main(String[] args) {
int a = 10;
a += 5;
System.out.println("a += 5: "
+ a);
a -= 3;
System.out.println("a -= 3: "
+ a);
a *= 2;
System.out.println("a *= 2: "
+ a);
a /= 4;
System.out.println("a /= 4: "
+ a);
}
}
Output:
a += 5: 15
a -= 3: 12
a *= 2: 24
a /= 4: 6
📌 Assignment 6: Ternary
Operator
Task:
Find the maximum of two
numbers using the ternary operator.
Solution:
public class TernaryOperator {
public static void main(String[] args) {
int a = 25, b = 40;
int max = (a > b) ? a : b;
System.out.println("Maximum
number: " + max);
}
}
Output:
Maximum number: 40
📌 Assignment 7: Operator
Precedence
Task:
Evaluate an expression
using operator precedence.
Solution:
public class OperatorPrecedence {
public static void main(String[] args) {
int result = 10 + 5 * 2 - 8 / 4;
System.out.println("Result: "
+ result);
}
}
Output:
Result: 17
📌 Assignment 8: Even or
Odd using Modulus Operator
Task:
Check if a number is even
or odd.
Solution:
public class EvenOddCheck {
public static void main(String[] args) {
int num = 21;
String result = (num % 2 == 0) ? "Even"
: "Odd";
System.out.println(num + " is
" + result);
}
}
Output:
21 is Odd
📌 Assignment 9: Swapping
Two Numbers Without Third Variable
Solution:
public class SwapNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After
swapping: a = " + a + ", b = " + b);
}
}
Output:
After swapping: a = 20, b = 10
📌 Assignment 10: Convert
Celsius to Fahrenheit
public class TemperatureConversion {
public static void main(String[] args) {
double celsius = 25;
double fahrenheit = (celsius * 9/5) + 32;
System.out.println("Temperature in
Fahrenheit: " + fahrenheit);
}
}
Output:
Temperature in Fahrenheit: 77.0
📌 Assignment 11: Largest
of Three Numbers
public class LargestOfThree {
public static void main(String[] args) {
int a = 15, b = 30, c = 25;
int max = (a > b) ? (a > c ? a :
c) : (b > c ? b : c);
System.out.println("Largest
number: " + max);
}
}
📌 Assignment 12: Bitwise
Left Shift
public class LeftShiftOperator {
public static void main(String[] args) {
int x = 5;
System.out.println("Left Shift:
" + (x << 2)); // Multiplies by 2^2
}
}
📌 Assignment 13: Find
Square of a Number Without Multiplication
public class SquareUsingBitwise {
public static void main(String[] args) {
int num = 4;
System.out.println("Square: "
+ (num << 1 + num << 2)); // 4x4 = 16
}
}
📌 Assignment 14: Check
Leap Year
public class LeapYearCheck {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = (year % 4 == 0
&& year % 100 != 0) || (year % 400 == 0);
System.out.println(year + " is a
Leap Year: " + isLeap);
}
}
📌 Assignment 15: Simple
Calculator
public class Calculator {
public static void main(String[] args) {
int a = 10, b = 5;
char op = '+';
int result = (op == '+') ? (a + b) :
(op == '-' ? (a - b) : 0);
System.out.println("Result: "
+ result);
}
}