Home  >  Article  >  Java  >  Operators in Java

Operators in Java

王林
王林Original
2024-07-17 22:48:51665browse

Operadores em Java

Types of Operators

Arithmetic:

  • Perform basic mathematical operations.

Bitwise: Operate on bits.
Relational: Compare values.
Logical: Perform logical operations.

Arithmetic Operators

Basic Operators:

  • Addition
  • Subtraction
  • Multiplication / Division % Modulo (remainder of division)

Unary Operators:

  • More unary
  • Less unary

Increment and Decrement Operators:
++ Increment (addition of 1)
-- Decrement (subtraction from 1)

Behavior of Arithmetic Operators

  • Operate on numeric and char types.

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.

  • Can be applied to integer and floating point types.
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn