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
Integer Division: 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); } }
Exit:
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: Increments/Decrements 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 the operation.
It is important to understand the behavior of operators to avoid logic errors in complex expressions.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3