Java Operators: Types and Examples Explained

使用して回答:GPT-4o-mini によってJava Q&A Hub

質問:

Introduce Java Operators and give examples

回答:

Java operators are special symbols that perform operations on variables and values. They can be categorized into several types based on their functionality. Here’s an overview of the main types of operators in Java, along with examples for each:

1. Arithmetic Operators

These operators are used to perform basic mathematical operations.

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the numerator by the denominator.
  • Modulus (%): Returns the remainder of a division operation.

Example:

int a = 10;
int b = 3;

System.out.println("Addition: " + (a + b));       // 13
System.out.println("Subtraction: " + (a - b));    // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b));        // 3
System.out.println("Modulus: " + (a % b));         // 1

2. Relational Operators

These operators are used to compare two values.

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right.
  • Less than (<): Checks if the left operand is less than the right.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right.

Example:

int x = 5;
int y = 10;

System.out.println("x == y: " + (x == y)); // false
System.out.println("x != y: " + (x != y)); // true
System.out.println("x > y: " + (x > y));   // false
System.out.println("x < y: " + (x < y));   // true
System.out.println("x >= y: " + (x >= y)); // false
System.out.println("x <= y: " + (x <= y)); // true

3. Logical Operators

These operators are used to combine multiple boolean expressions.

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one of the operands is true.
  • Logical NOT (!): Reverses the logical state of its operand.

Example:

boolean a1 = true;
boolean a2 = false;

System.out.println("a1 && a2: " + (a1 && a2)); // false
System.out.println("a1 || a2: " + (a1 || a2)); // true
System.out.println("!a1: " + (!a1));            // false

4. Bitwise Operators

These operators perform operations on bits and are used for bit manipulation.

  • AND (&): Performs a bitwise AND operation.
  • OR (|): Performs a bitwise OR operation.
  • XOR (^): Performs a bitwise exclusive OR operation.
  • Complement (~): Inverts all the bits.
  • Left Shift (<<): Shifts bits to the left, filling with zeros.
  • Right Shift (>>): Shifts bits to the right.

Example:

int p = 5;  // 0101 in binary
int q = 3;  // 0011 in binary

System.out.println("p & q: " + (p & q)); // 1 (0001)
System.out.println("p | q: " + (p | q)); // 7 (0111)
System.out.println("p ^ q: " + (p ^ q)); // 6 (0110)
System.out.println("~p: " + (~p));        // -6 (inverts bits)
System.out.println("p << 1: " + (p << 1)); // 10 (1010)
System.out.println("p >> 1: " + (p >> 1)); // 2 (0010)

5. Assignment Operators

These operators are used to assign values to variables.

  • Simple assignment (=): Assigns the right operand to the left operand.
  • Add and assign (+=): Adds the right operand to the left operand and assigns the result.
  • Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result.
  • Multiply and assign (*=): Multiplies the left operand by the right operand and assigns the result.
  • Divide and assign (/=): Divides the left operand by the right operand and assigns the result.
  • Modulus and assign (%=): Takes the modulus using two operands and assigns the result.

Example:

int num = 10;
num += 5; // num = num + 5
System.out.println("After += : " + num); // 15

num -= 3; // num = num - 3
System.out.println("After -= : " + num); // 12

num *= 2; // num = num * 2
System.out.println("After *= : " + num); // 24

num /= 4; // num = num / 4
System.out.println("After /= : " + num); // 6

num %= 5; // num = num % 5
System.out.println("After %= : " + num); // 1

6. Unary Operators

These operators operate on a single operand.

  • Unary plus (+): Indicates a positive value (usually optional).
  • Unary minus (-): Negates the value.
  • Increment (++): Increases the value by 1.
  • Decrement (--): Decreases the value by 1.

Example:

int value = 10;

System.out.println("Unary plus: " + (+value)); // 10
System.out.println("Unary minus: " + (-value)); // -10

value++; // Increment
System.out.println("After increment: " + value); // 11

value--; // Decrement
System.out.println("After decrement: " + value); // 10

7. Ternary Operator

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

Syntax: condition ? expression1 : expression2

Example:

int a = 5;
int b = 10;
int max = (a > b) ? a : b; // If a is greater than b, max is a; otherwise, max is b
System.out.println("Max value: " + max); // 10

Conclusion

Java operators are essential for performing various operations in programming. Understanding how to use these operators effectively is crucial for writing efficient and functional Java code.