Types of Operators
Arithmetic:
Bitwise: Operate on bits.
Relational: Compare values.
Logical: Perform logical operations.
Arithmetic Operators
Basic Operators:
Unary Operators:
Increment and Decrement Operators:
++ Increment (addition of 1)
-- Decrement (subtraction from 1)
Behavior of Arithmetic Operators
Division of Integers: Truncates the remainder.
Example: 10 / 3 results in 3.
Module Operator: Generates the remainder of the division.
Example: 10 % 3 results in 1.
class ModDemo { public static void main(String args[]) { int iresult = 10 / 3; int irem = 10 % 3; double dresult = 10.0 / 3.0; double drem = 10.0 % 3.0; System.out.println("Result and remainder of 10 / 3: " + iresult + " " + irem); System.out.println("Result and remainder of 10.0 / 3.0: " + dresult + " " + drem); } }
Output:
Result and remainder of 10 / 3: 3 1
Result and remainder of 10.0 / 3.0: 3.3333333333333335 1.0
Increment and Decrement
Increment (++): Adds 1 to the operand.
Example: x = x + 1; is equivalent to x++;
Decrement (--): Subtracts 1 from the operand.
Example: x = x - 1; is equivalent to x--;
Prefixed and Postfixed Forms:
Prefixed: Increment/Decrement before using the value in the expression.
Example: ++x
Postfix: Uses the value in the expression before incrementing/decrementing.
Example: x++
int x = 10; int y = ++x; // y será 11, x será 11
Summary of Key Points
Java has operators for mathematical, logical, relational and bitwise operations.
Arithmetic operators include +, -, *, /, %, ++, --.
% operator can be applied to integer and floating point types.
Increment (++) and decrement (--) have prefix and postfix forms that affect the order of operation.
It is important to understand the behavior of operators to avoid logic errors in complex expressions.
The above is the detailed content of Operators in Java. For more information, please follow other related articles on the PHP Chinese website!