Introduction to Java | Programming Basics
Operators in Java
In Java, operators are special symbols or keywords used to perform operations on variables and values. Java has a rich set of operators that can be classified into various categories based on their functionality.
Types of Operators in Java
Java operators are divided into several categories:
- Arithmetic Operators: Used for basic arithmetic operations like addition, subtraction, multiplication, etc.
- Relational (Comparison) Operators: Used to compare two values.
- Logical Operators: Used to perform logical operations, often with boolean values.
- Assignment Operators: Used to assign values to variables.
- Unary Operators: Operate on a single operand.
- Bitwise Operators: Used for manipulating individual bits of integer data types.
- Conditional (Ternary) Operator: A shorthand for an if-else statement.
- Instanceof Operator: Used to test whether an object is an instance of a specific class or subclass.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. These include:
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus (remainder)
Example: Arithmetic Operators
Code Example
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("a + b = " + (a + b)); // Addition
System.out.println("a - b = " + (a - b)); // Subtraction
System.out.println("a * b = " + (a * b)); // Multiplication
System.out.println("a / b = " + (a / b)); // Division
System.out.println("a % b = " + (a % b)); // Modulus
}
}
Output
a - b = 5
a * b = 50
a / b = 2
a % b = 0
2. Relational (Comparison) Operators
Relational operators are used to compare two values. They return a boolean value (true or false) depending on the comparison.
- == Equal to
- != Not equal to
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
Example: Relational Operators
Code Example
public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 5;
System.out.println("x == y: " + (x == y)); // Equal to
System.out.println("x != y: " + (x != y)); // Not equal to
System.out.println("x > y: " + (x > y)); // Greater than
System.out.println("x < y: " + (x < y)); // Less than
System.out.println("x >= y: " + (x >= y)); // Greater than or equal to
System.out.println("x <= y: " + (x <= y)); // Less than or equal to
}
}
Output
x != y: true
x > y: true
x < y: false
x >= y: true
x <= y: false
3. Logical Operators
Logical operators are used to combine multiple boolean expressions and return a boolean value. The commonly used logical operators are:
- && Logical AND
- || Logical OR
- ! Logical NOT
Example: Logical Operators
Code Example
public class LogicalExample {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println("a && b: " + (a && b)); // Logical AND
System.out.println("a || b: " + (a || b)); // Logical OR
System.out.println("!a: " + !a); // Logical NOT
}
}
Output
a || b: true
!a: false
4. Assignment Operators
Assignment operators are used to assign values to variables. Commonly used assignment operators include:
- = Simple assignment
- += Add and assign
- -= Subtract and assign
- *= Multiply and assign
- /= Divide and assign
- %= Modulus and assign
Example: Assignment Operators
Code Example
public class AssignmentExample {
public static void main(String[] args) {
int num = 10;
num += 5; // Add and assign
System.out.println("num += 5: " + num); // num = 15
num -= 3; // Subtract and assign
System.out.println("num -= 3: " + num); // num = 12
num *= 2; // Multiply and assign
System.out.println("num *= 2: " + num); // num = 24
num /= 4; // Divide and assign
System.out.println("num /= 4: " + num); // num = 6
num %= 3; // Modulus and assign
System.out.println("num %= 3: " + num); // num = 0
}
}
Output
num -= 3: 12
num *= 2: 24
num /= 4: 6
num %= 3: 0
5. Unary Operators
Unary operators operate on a single operand to perform various operations, such as increment, decrement, and negation.
- + Unary plus (indicates positive value)
- - Unary minus (negates the value)
- ++ Increment operator (increases the value by 1)
- -- Decrement operator (decreases the value by 1)
- ! Logical NOT (reverses boolean value)
Example: Unary Operators
Code Example
public class UnaryExample {
public static void main(String[] args) {
int num = 5;
System.out.println("++num: " + ++num); // Pre-increment
System.out.println("num++: " + num++); // Post-increment
System.out.println("--num: " + --num); // Pre-decrement
System.out.println("num--: " + num--); // Post-decrement
System.out.println("!true: " + !true); // Logical NOT
}
}
Output
num++: 6
--num: 5
num--: 5
!true: false
6. Conditional (Ternary) Operator
The ternary operator is a shorthand for an if-else statement. It is written as:
condition ? expression1 : expression2;
Example: Conditional Operator
Code Example
public class TernaryExample {
public static void main(String[] args) {
int x = 10, y = 5;
String result = (x > y) ? "x is greater" : "y is greater";
System.out.println(result);
}
}