Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In this tutorial, we will discuss arithmetic operators in Java. Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
The addition operator (+) is used to add two values together.
int a = 10; int b = 20; int sum = a + b; // sum = 30
The subtraction operator (-) is used to subtract one value from another.
int a = 30; int b = 10; int difference = a - b; // difference = 20
The multiplication operator (*) is used to multiply two values.
int a = 5; int b = 4; int product = a * b; // product = 20
The division operator (/) is used to divide one value by another. When dividing integers, the result will be an integer, and any remainder will be discarded.
int a = 15; int b = 4; int quotient = a / b; // quotient = 3 (remainder 3 is discarded)
The modulus operator (%) is used to find the remainder after division of one value by another.
int a = 15; int b = 4; int remainder = a % b; // remainder = 3
These arithmetic operators can also be used with floating-point numbers (float and double). Be cautious when working with floating-point numbers due to the potential for loss of precision.
It's important to note that when performing arithmetic operations with mixed data types, Java will perform implicit type casting to ensure compatibility. For instance, if you multiply an int by a double, the int will be cast to a double before the operation.
Java Addition Operator Example:
The addition operator (+
) is used to add two numeric values.
int sum = 5 + 3; // Result: 8
Subtraction Operator in Java:
The subtraction operator (-
) is used to subtract one numeric value from another.
int difference = 10 - 4; // Result: 6
Multiplication Operator in Java:
The multiplication operator (*
) is used to multiply two numeric values.
int product = 3 * 7; // Result: 21
Division Operator Examples in Java:
The division operator (/
) is used to divide one numeric value by another.
int quotient = 20 / 4; // Result: 5
Java Modulo Operator Usage:
The modulo operator (%
) returns the remainder of the division.
int remainder = 15 % 4; // Result: 3
Compound Assignment Operators in Java: Compound assignment operators combine an arithmetic operation with assignment.
int x = 10; x += 5; // Equivalent to x = x + 5
Java Increment and Decrement Operators:
Increment (++
) and decrement (--
) operators increase or decrease a variable by 1.
int count = 5; count++; // Increment by 1, count becomes 6
Order of Precedence in Java Arithmetic Operators: The order of precedence determines the sequence in which arithmetic operations are performed.
int result = 2 + 3 * 4; // Result: 14 (multiplication first)
Java Arithmetic Operators and Type Casting: Automatic type casting may occur in expressions involving different data types.
int intValue = 5; double doubleValue = 2.5; double result = intValue + doubleValue; // Result: 7.5 (int implicitly cast to double)
Mixing Data Types with Arithmetic Operators in Java: Be cautious when mixing data types, as unexpected results may occur.
int num = 10; double result = num / 3; // Result: 3.0 (int divided by int, then implicitly cast to double)
Overflow and Underflow with Java Arithmetic Operators: Overflow occurs when the result of an arithmetic operation exceeds the maximum value for the data type.
int maxInt = Integer.MAX_VALUE; int overflow = maxInt + 1; // Overflow
Java Arithmetic Operators in Expressions: Arithmetic operators are commonly used in expressions to perform calculations.
int result = (3 + 5) * 2; // Result: 16
Arithmetic Operators in Java Control Flow Statements: Arithmetic operations are often used within control flow statements to determine branching.
int score = 75; if (score >= 60) { System.out.println("Passed"); } else { System.out.println("Failed"); }
Handling Exceptions with Arithmetic Operators in Java: Handle potential division by zero exceptions when using the division operator.
int numerator = 10; int denominator = 0; try { int result = numerator / denominator; // Division by zero exception } catch (ArithmeticException e) { System.out.println("Division by zero is not allowed"); }
Java Arithmetic Operators and Floating-Point Numbers: Arithmetic operators work with floating-point numbers, but be aware of precision issues.
double result = 1.0 / 3.0; // Result: 0.3333333333333333
Java Bitwise Operators and Arithmetic Operators: Bitwise operators and arithmetic operators serve different purposes but can be used together in some scenarios.
int bitwiseResult = 5 + (1 << 2); // Result: 9 (bitwise left shift and addition)
Using Parentheses with Java Arithmetic Operators: Parentheses can be used to control the order of operations and ensure the desired calculation sequence.
int result = (2 + 3) * 4; // Result: 20
Java Arithmetic Operators in Conditional Statements: Arithmetic operators are often used in conditional statements to evaluate expressions.
int num1 = 10; int num2 = 15; if (num1 + num2 > 20) { System.out.println("Sum is greater than 20"); } else { System.out.println("Sum is not greater than 20"); }