Tutorials Logic, IN info@tutorialslogic.com

Operators in Java Arithmetic, Logical, Bitwise

Arithmetic and Remainder Operators

Read a Java expression by grouping precedence first, then tracing operand types, promotions, side effects, and whether short-circuit evaluation skips the right operand.

Arithmetic operators work with numeric values. The remainder operator (%) is especially useful for checking even numbers, cycling indexes, and extracting digits.

Arithmetic Operators

Arithmetic Operators
public class ArithmeticOperators {
    public static void main(String[] args) {
        int a = 17;
        int b = 5;

        System.out.println(a + b); // 22
        System.out.println(a - b); // 12
        System.out.println(a * b); // 85
        System.out.println(a / b); // 3, integer division
        System.out.println(a % b); // 2, remainder

        System.out.println(a % 2 == 0 ? "Even" : "Odd");
    }
}
  • Integer division drops the fractional part.
  • Use double operands when you need decimal division.

Relational and Logical Operators

Relational operators compare values and produce boolean results. Logical operators combine boolean expressions. Java uses short-circuit evaluation for && and ||.

Logical Conditions

Logical Conditions
public class LogicalOperators {
    public static void main(String[] args) {
        int age = 22;
        boolean hasId = true;

        boolean canEnter = age >= 18 && hasId;
        boolean needsHelp = age < 18 || !hasId;

        System.out.println(canEnter);
        System.out.println(needsHelp);
    }
}
  • && stops when the left side is false.
  • || stops when the left side is true.

Assignment, Increment, and Compound Operators

Assignment stores a value in a variable. Compound assignment combines an operation and assignment. Increment and decrement are compact but should be used carefully in complex expressions.

Assignment Operators

Assignment Operators
public class AssignmentOperators {
    public static void main(String[] args) {
        int score = 10;
        score += 5;  // score = score + 5
        score *= 2;  // score = score * 2

        int post = score++;
        int pre = ++score;

        System.out.println(score);
        System.out.println(post);
        System.out.println(pre);
    }
}
  • Prefer simple increment statements over clever expressions.
  • Compound assignment performs an implicit cast.

Bitwise, Ternary, and instanceof Operators

Bitwise operators work at the binary level. The ternary operator is a compact if-else expression. The instanceof operator checks whether an object is compatible with a type.

  • Use ternary for short value selection, not large business logic.
  • Use instanceof before safe downcasting when needed.
  • Use bitwise operators mostly for flags, masks, and low-level logic.

Ternary and instanceof

Ternary and instanceof
public class SpecialOperators {
    public static void main(String[] args) {
        int marks = 76;
        String result = marks >= 35 ? "Pass" : "Fail";
        System.out.println(result);

        Object value = "Core Java";
        if (value instanceof String text) {
            System.out.println(text.toUpperCase());
        }
    }
}

Reading Java expressions without guessing precedence

Operators perform calculations, comparisons, assignments, and logical decisions. Java includes arithmetic operators such as + and *, relational operators such as > and ==, logical operators such as && and ||, assignment operators such as +=, and the ternary operator for compact conditional values.

The difficult part is usually precedence and short-circuit behavior. Multiplication runs before addition, && can skip the right side when the left side is false, and || can skip the right side when the left side is true. Parentheses are not a weakness; they are often the clearest way to show the intended order.

  • Use parentheses when an expression mixes multiple operator types.
  • Remember that = assigns while == compares primitive values.
  • Use equals() for object content comparison such as String values.
  • Understand short-circuiting before putting method calls inside && or ||.

Discount calculation with logical and ternary operators

Discount calculation with logical and ternary operators
double total = 1250;
boolean member = true;

double payable = member && total >= 1000
    ? total * 0.90
    : total;

System.out.println(payable);
Before you move on

Operators in Java Arithmetic, Logical, Bitwise Mastery Check

5 checks
  • The remainder operator (%) is especially useful for checking even numbers, cycling indexes, and extracting digits.
  • Relational operators compare values and produce boolean results.
  • Java uses short-circuit evaluation for && and ||.
  • Assignment stores a value in a variable.
  • Compound assignment combines an operation and assignment.

Core Java Questions Learners Ask

&& stops when the left side is false. & evaluates both sides.

When both operands are integers, Java produces an integer result. Cast an operand to double when you need a fraction.

No. Use equals for contents; == checks whether both references point to the same object.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.