Java Basics

Java Operators

Learn about arithmetic, relational, logical, bitwise, assignment, and other operators in Java with examples.

What are Operators?

Operators are special symbols that perform operations on variables and values. Java supports a rich set of operators categorized by their functionality.

Arithmetic Operators

Used for mathematical calculations:

java
public class ArithmeticDemo {
    public static void main(String[] args) {
        int a = 20, b = 7;

        System.out.println("a + b = " + (a + b));   // 27 (Addition)
        System.out.println("a - b = " + (a - b));   // 13 (Subtraction)
        System.out.println("a * b = " + (a * b));   // 140 (Multiplication)
        System.out.println("a / b = " + (a / b));   // 2 (Integer Division)
        System.out.println("a % b = " + (a % b));   // 6 (Modulus / Remainder)
    }
}

Note: Integer division truncates the decimal. Use double for precise division: (double) a / b gives 2.857...

Unary Operators

Operate on a single operand:

OperatorDescriptionExample
+Unary plus+5
-Unary minus (negate)-5
++Increment by 1i++ or ++i
--Decrement by 1i-- or --i
!Logical NOT!truefalse

Pre vs Post Increment

java
int a = 5;
System.out.println(a++);   // Prints 5, THEN a becomes 6 (post-increment)
System.out.println(++a);   // a becomes 7, THEN prints 7 (pre-increment)

Relational (Comparison) Operators

Return true or false:

java
int x = 10, y = 20;

System.out.println(x == y);   // false (equal to)
System.out.println(x != y);   // true  (not equal to)
System.out.println(x > y);    // false (greater than)
System.out.println(x < y);    // true  (less than)
System.out.println(x >= 10);  // true  (greater than or equal)
System.out.println(x <= 5);   // false (less than or equal)

Logical Operators

Combine boolean expressions:

java
boolean a = true, b = false;

System.out.println(a && b);   // false (AND — both must be true)
System.out.println(a || b);   // true  (OR — at least one true)
System.out.println(!a);       // false (NOT — reverses)

Short-Circuit Evaluation

Java uses short-circuit evaluation — it stops checking as soon as the result is determined:

java
// If first condition is false, second is NOT evaluated (&&)
if (x != 0 && 100 / x > 5) { ... }

// If first condition is true, second is NOT evaluated (||)
if (isAdmin || checkPermissions()) { ... }

Assignment Operators

java
int x = 10;        // Assign

x += 5;     // x = x + 5  → 15
x -= 3;     // x = x - 3  → 12
x *= 2;     // x = x * 2  → 24
x /= 4;     // x = x / 4  → 6
x %= 4;     // x = x % 4  → 2

Bitwise Operators

Operate on individual bits:

OperatorNameExample (5 & 3)
&AND0101 & 0011 = 0001 (1)
|OR0101 | 0011 = 0111 (7)
^XOR0101 ^ 0011 = 0110 (6)
~NOT~0101 = 1010
<<Left Shift5 << 1 = 10
>>Right Shift5 >> 1 = 2

Ternary Operator

Shorthand for simple if-else:

java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);  // Adult

instanceof Operator

Checks if an object is an instance of a class:

java
String name = "Java";
System.out.println(name instanceof String);   // true
System.out.println(name instanceof Object);   // true

Operator Precedence

From highest to lowest:

PriorityOperators
1() (parentheses)
2++, --, !, ~ (unary)
3*, /, %
4+, -
5<<, >>, >>>
6<, <=, >, >=, instanceof
7==, !=
8&, ^, |
9&&, ||
10? : (ternary)
11=, +=, -=, etc.

Tip: When in doubt, use parentheses to make precedence explicit and code readable.

Practical Example

java
public class BillCalculator {
    public static void main(String[] args) {
        double subtotal = 1250.00;
        double taxRate = 0.18;
        double discount = (subtotal > 1000) ? 0.10 : 0.0;

        double discountAmount = subtotal * discount;
        double afterDiscount = subtotal - discountAmount;
        double tax = afterDiscount * taxRate;
        double total = afterDiscount + tax;

        System.out.printf("Subtotal:  ₹%.2f%n", subtotal);
        System.out.printf("Discount:  -₹%.2f (%.0f%%)%n", discountAmount, discount * 100);
        System.out.printf("Tax:       +₹%.2f (%.0f%% GST)%n", tax, taxRate * 100);
        System.out.printf("Total:     ₹%.2f%n", total);
    }
}

Output:

Subtotal:  ₹1250.00
Discount:  -₹125.00 (10%)
Tax:       +₹202.50 (18% GST)
Total:     ₹1327.50

Summary

  • Java provides arithmetic, relational, logical, bitwise, assignment, and ternary operators
  • ++/-- have pre and post forms with different evaluation order
  • Logical operators use short-circuit evaluation for efficiency
  • Assignment operators (+=, -=, etc.) combine operation with assignment
  • Use parentheses to clarify operator precedence
  • The instanceof operator checks type compatibility at runtime