Java Arithmetic Operators
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. Here’s a detailed explanation of each arithmetic operator along with examples.
1. Addition Operator (+
)
The addition operator adds two operands. If either operand is a string, it concatenates them.
Example:
public class AdditionExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int sum = a + b; // Addition of two integers
System.out.println("Sum: " + sum); // Output: Sum: 15
String str1 = "Hello, ";
String str2 = "World!";
String greeting = str1 + str2; // String concatenation
System.out.println(greeting); // Output: Hello, World!
}
}
2. Subtraction Operator (-
)
The subtraction operator subtracts the second operand from the first.
Example:
public class SubtractionExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int difference = a - b; // Subtraction of two integers
System.out.println("Difference: " + difference); // Output: Difference: 5
}
}
3. Multiplication Operator (*
)
The multiplication operator multiplies two operands.
Example:
public class MultiplicationExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int product = a * b; // Multiplication of two integers
System.out.println("Product: " + product); // Output: Product: 50
}
}
4. Division Operator (/
)
The division operator divides the first operand by the second. If both operands are integers, it performs integer division (truncating any decimal portion). If either operand is a floating-point number, it performs floating-point division.
Example:
public class DivisionExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
int integerDivision = a / b; // Integer division
System.out.println("Integer Division: " + integerDivision); // Output: Integer Division: 3
double c = 10.0;
double d = 3.0;
double floatingPointDivision = c / d; // Floating-point division
System.out.println("Floating-point Division: " + floatingPointDivision); // Output: Floating-point Division: 3.3333333333333335
}
}
5. Modulus Operator (%
)
The modulus operator finds the remainder of the division of the first operand by the second. It is particularly useful for checking if a number is even or odd.
Example:
public class ModulusExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
int remainder = a % b; // Remainder of division
System.out.println("Remainder: " + remainder); // Output: Remainder: 1
// Checking if a number is even or odd
if (a % 2 == 0) {
System.out.println(a + " is even."); // Output: 10 is even.
} else {
System.out.println(a + " is odd.");
}
}
}
Summary
In Java, arithmetic operators are essential for performing mathematical calculations and manipulations. They are easy to use and understand, allowing developers to write efficient code for various applications. Be mindful of the data types of the operands, especially when performing division, as they can significantly impact the results.