Introduction
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators are fundamental for manipulating numerical data and are frequently used in Java programming.
What are Arithmetic Operators?
Arithmetic operators are symbols used within expressions to perform basic mathematical operations. They operate on numerical values (constants and variables) and return a single numerical value.
Types of Arithmetic Operators in Java
Java provides the following arithmetic operators:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus (
%
) - Increment (
++
) - Decrement (
--
)
1. Addition (+
)
The addition operator adds two operands.
Example:
public class AdditionExample {
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 second operand from the first.
Example:
public class SubtractionExample {
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.
Example:
public class MultiplicationExample {
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 first operand by the second. Note that division by zero will throw an ArithmeticException
.
Example:
public class DivisionExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
int quotient = a / b;
System.out.println("Quotient: " + quotient); // Output: Quotient: 2
}
}
5. Modulus (%
)
The modulus operator returns the remainder when the first operand is divided by the second.
Example:
public class ModulusExample {
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 an operand by 1. It can be used as a prefix (++a
) or a suffix (a++
).
Example:
public class IncrementExample {
public static void main(String[] args) {
int a = 10;
++a; // Prefix increment
System.out.println("Prefix Increment: " + a); // Output: Prefix Increment: 11
a = 10;
a++; // Suffix increment
System.out.println("Suffix Increment: " + a); // Output: Suffix Increment: 11
}
}
7. Decrement (--
)
The decrement operator decreases the value of an operand by 1. It can be used as a prefix (--a
) or a suffix (a--
).
Example:
public class DecrementExample {
public static void main(String[] args) {
int a = 10;
--a; // Prefix decrement
System.out.println("Prefix Decrement: " + a); // Output: Prefix Decrement: 9
a = 10;
a--; // Suffix decrement
System.out.println("Suffix Decrement: " + a); // Output: Suffix Decrement: 9
}
}
Conclusion
Arithmetic operators in Java are used to perform basic mathematical operations. They allow for efficient manipulation of numerical data and are integral to many programming tasks. By understanding and using these operators correctly, you can perform calculations and manage data effectively in your Java programs.
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment