What is Java Operator?
In Java programming, an operator is a special symbol that performs operations on variables and values. Operators are used to manipulate data and variables in expressions. Java operators can be categorized into several types:
Arithmetic Operators
In Java, arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators work with primitive data types like int, float, double, long, and others.
List of Arithmetic Operators #
- Addition (+)
- Subtraction (–)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Increment (++)
- Decrement (—)
Examples and Explanations #
1. Addition (+)
The addition operator adds two operands.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 30
2. Subtraction (–)
The subtraction operator subtracts the right operand from the left operand.
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 10;
int difference = a - b;
System.out.println("Difference: " + difference);
}
}
Output:
Difference: 10
3. Multiplication (*)
The multiplication operator multiplies two operands.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int product = a * b;
System.out.println("Product: " + product);
}
}
Output:
Product: 200
4. Division (/)
The division operator divides the left operand by the right operand. For integer division, it returns the quotient and discards the remainder.
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 10;
int quotient = a / b;
System.out.println("Quotient: " + quotient);
double x = 20.0;
double y = 3.0;
double result = x / y;
System.out.println("Result: " + result);
}
}
Output:
Quotient: 2
Result: 6.666666666666667
5. Modulus (%)
The modulus operator returns the remainder of the division of the left operand by the right operand.
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 3;
int remainder = a % b;
System.out.println("Remainder: " + remainder);
}
}
Output:
Remainder: 2
6. Increment (++)
The increment operator increases the value of a variable by 1. It can be used as a prefix or postfix.
- Prefix (++variable): Increments the value before using it in an expression.
- Postfix (variable++): Increments the value after using it in an expression.
public class Main {
public static void main(String[] args) {
int a = 10;
System.out.println("Prefix Increment: " + (++a));
a = 10;
System.out.println("Postfix Increment: " + (a++));
System.out.println("After Postfix Increment: " + a);
}
}
Output:
Prefix Increment: 11
Postfix Increment: 10
After Postfix Increment: 11
7. Decrement (—)
The decrement operator decreases the value of a variable by 1. It can be used as a prefix or postfix.
- Prefix (–variable): Decrements the value before using it in an expression.
- Postfix (variable–): Decrements the value after using it in an expression.
public class Main {
public static void main(String[] args) {
int a = 10;
System.out.println("Prefix Decrement: " + (--a));
a = 10;
System.out.println("Postfix Decrement: " + (a--));
System.out.println("After Postfix Decrement: " + a);
}
}
Output:
Prefix Decrement: 9
Postfix Decrement: 10
After Postfix Decrement: 9
Summary #
Arithmetic operators are fundamental in performing mathematical calculations in Java programs. Understanding how to use each operator and the difference between prefix and postfix increment/decrement is essential for writing effective and accurate code.