Chuyển đến nội dung chính

Java Operators

Java operators are special symbols that perform operations on variables and values. They are used to manipulate data and variables in expressions. Java provides a wide array of operators categorized into several types based on their functionality.

Types of Java Operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators
  • Bitwise Operators
  • Ternary Operator

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus)

Example

int a = 10;
int b = 5;
System.out.println("Addition: " + (a + b));      // Output: 15
System.out.println("Subtraction: " + (a - b));   // Output: 5
System.out.println("Multiplication: " + (a * b));// Output: 50
System.out.println("Division: " + (a / b));      // Output: 2
System.out.println("Modulus: " + (a % b));       // Output: 0

Relational Operators

Relational operators are used to compare two values.

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Example

int a = 10;
int b = 5;
System.out.println(a == b); // Output: false
System.out.println(a > b);  // Output: true
System.out.println(a < b);  // Output: false

Logical Operators

Logical operators are used to combine multiple boolean expressions.

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Example

boolean x = true;
boolean y = false;
System.out.println(x && y); // Output: false
System.out.println(x || y); // Output: true
System.out.println(!x);     // Output: false

Assignment Operators

Assignment operators are used to assign values to variables.

  • = (Simple assignment)
  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)
  • %= (Modulus and assign)

Example

int a = 10;
a += 5;  // Equivalent to a = a + 5
System.out.println(a); // Output: 15

Unary Operators

Unary operators operate on a single operand.

  • + (Unary plus)
  • - (Unary minus)
  • ++ (Increment)
  • -- (Decrement)

Example

int a = 10;
System.out.println(++a); // Output: 11
System.out.println(--a); // Output: 10

Bitwise Operators

Bitwise operators perform operations on bits.

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left shift)
  • >> (Right shift)
  • >>> (Unsigned right shift)

Example

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011
System.out.println(a & b); // Output: 1 (Binary: 0001)

Ternary Operator

The ternary operator is a shorthand form of the if-else statement.

  • ? : (Ternary)

Example

int a = 10;
int b = 5;
int max = (a > b) ? a : b;
System.out.println(max); // Output: 10

Tips and Best Practices

  • Use Parentheses: Use parentheses to ensure the desired order of operations.
  • Readable Code: Avoid complex expressions that can make the code less readable.
  • Avoid Division by Zero: Always check for zero before performing division operations.
  • Use Logical Operators with Care: Ensure logical operators are used correctly to avoid unexpected results.
  • Bitwise Operations: Be cautious when using bitwise operators, especially with signed numbers.