"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Operators in Java

Operators in Java

Published on 2024-08-01
Browse:987

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:

  • Plus unary
  • Minus unary

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

Behavior of Arithmetic Operators

  • Operate on numeric and char types.

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.

  • 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);
    }
}

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.

Release Statement This article is reproduced at: https://dev.to/devsjavagirls/operadores-em-java-2ced?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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