Introduction to Java Operators

In Java, operators are special symbols that perform operations on one or more operands. Operands are the values or variables on which the operations are performed. Understanding operators is crucial because they are the building blocks for any computation, comparison, or logical decision-making in your Java programs.

For example, in the expression int sum = 10 + 5;, + is the operator, and 10 and 5 are the operands. The result of the operation (15) is then assigned to the sum variable.

Operators can be classified based on the number of operands they take:

  • Unary Operators: Operate on a single operand (e.g., ++, --, !).

  • Binary Operators: Operate on two operands (e.g., +, -, *, /).

  • Ternary Operators: Operate on three operands (only one in Java: ?:).

Categories of Operators

Java provides a rich set of operators, which can be broadly categorized as follows:

Arithmetic Operators

These operators are used to perform basic mathematical calculations.

Note on Integer Division: When dividing two integers, Java performs integer division, which truncates any decimal part. For floating-point division, at least one of the operands must be a floating-point type (float or double). Example: 10 / 3.0 would result in 3.333...

Assignment Operators

Assignment operators are used to assign a value to a variable. The basic assignment operator is =. Compound assignment operators combine an arithmetic operation with assignment. The value of 3 in the example table is simply for example purposes and can be changed to fit your program’s needs

Comparison (Relational) Operators

These operators compare two operands and return a boolean result (true or false).

Note: the quotes around == is simply due to the image tool not allowing me to input as I wanted to, in practice there are no quotes around the == operator.

Logical Operators

Logical operators are fundamental for controlling program flow by combining or negating boolean expressions. They always yield a boolean result (true or false). These operators can become some of the most complex to work with due to the amount of chaining you can do. There are whole classes dedicated to logic, so I will only be covering the basics of logical operators.

  • && (Logical AND)

    • Purpose: Returns true if both operands are true. If even one operand is false, the result is false

  • || (Logical OR)

    • Purpose: Returns true if at least one of the operands is true. It returns false only if both operands are false

  • ! (Logical NOT)

    • Purpose: This is a unary operator that inverts the boolean value of its single operand. true becomes false, and false becomes true

Difference between && vs. & and || vs. |: Java also has single & (Bitwise AND) and | (Bitwise OR) operators. While they can operate on boolean expressions, they do not short-circuit. This means both operands will always be evaluated, even if the result can be determined from the first operand alone. For boolean operations, && and || are almost always preferred for efficiency and to prevent side effects or errors from the second operand's evaluation. The single & and | are primarily used for bitwise manipulations on integer types.

Key Takeaways

  • Operators allow for data to be changed and compared

  • The order of operations is the same as it is in math (parenthesis, exponent, multiplication, division, addition, subtraction)

  • Booleans they only deal with true or false

Quiz