Java + Spring Boot track

Lesson 03 · Part 1: Basics

Operators and Expressions

An operator is a symbol that takes a value (operand) and creates a new value. Java's operators look familiar because they originated from C/C++, but they have…

Part 1: Basics4 min readFree lesson

An operator is a symbol that takes a value (operand) and creates a new value. Java's operators look familiar because they originated from C/C++, but they have pitfalls such as object comparison (== vs .equals()) and integer/real division. We summarize in this section.

Learning Objectives

  • Arithmetic, comparison, logic, bit, ternary, and increment/decrement operators are used separately.
  • Understand the left-to-right evaluation rules of + when combining String
  • Clearly know the difference between == and .equals()
  • Understand what short-circuit evaluation (&& / ||) is

Key concepts

1) Arithmetic/Comparison/Logic

int a = 10, b = 3;
System.out.println(a + b);   // 13
System.out.println(a / b);   //3 (integer division!)
System.out.println(a % b);   // 1
System.out.println(a > b);   // true
System.out.println(a == 10 && b > 0);   // true

2) Increment/decrement operator

int x = 5;
System.out.println(x++);   //5 (increases after use)
System.out.println(++x);   //7 (use after increase)

x++ and ++x have different result values. Be careful because readability will decrease if you mix them in a formula.

3) Ternary operator

int score = 75;
String result = (score >= 60) ? "passed" : "failed";

It is a short form of if/else. This is useful when setting values ​​in one line.

4) == vs .equals()

String a = "hello";
String b = new String("hello");
System.out.println(a == b);         //false (references are different)
System.out.println(a.equals(b));    //true (content is the same)

Always compare objects (especially String/Integer/List, etc.) with .equals(). == is for checking equality between primitives or references.

5) Bitwise operators

int mask = 0b1010;
int val  = 0b1100;
System.out.println(mask & val);  // 8   (AND)
System.out.println(mask | val);  // 14  (OR)
System.out.println(mask ^ val);  // 6   (XOR)
System.out.println(mask << 1);   //20 (left shift)

It is encountered in places such as flag handling, hashing, and encryption.

See as example

Example 1 — Arithmetic.java: Arithmetic and division pitfalls

public class Arithmetic {
    public static void main(String[] args) {
        int a = 17, b = 5;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);        //Integer division
        System.out.println(a % b);
        System.out.println((double) a / b); //Real division
    }
}

Execution result

22
12
85
3
2
3.4

Note: int / int is always an integer. If you need a real number, cast one side as double.

Example 2 — LogicAndTernary.java: Comparison, logic, ternary

public class LogicAndTernary {
    public static void main(String[] args) {
        int score = 75;
        boolean passed = score >= 60;
        System.out.println("Passed?" + passed);

        String grade = (score >= 90) ? "A"
                     : (score >= 80) ? "B"
                     : (score >= 70) ? "C"
                     : (score >= 60) ? "D"
                     : "F";
        System.out.println("Rating:" + grade);

        boolean ok = score > 0 && score <= 100;
        System.out.println("Effective range?" + ok);
    }
}

Execution result

Passed? true
Grade: C
Effective range? true

Note: && and || are short-circuit. Safe null check is possible like a != null && a.length() > 0.

Example 3 — EqualsVsEqEq.java: Reference comparison vs content comparison

public class EqualsVsEqEq {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = new String("Java");
        System.out.println("a == b      : " + (a == b));
        System.out.println("a == c      : " + (a == c));
        System.out.println("a.equals(c) : " + a.equals(c));

        Integer x = 100;
        Integer y = 100;
        Integer z = 200;
        Integer w = 200;
        System.out.println("100==100? " + (x == y)); //true as cache
        System.out.println("200==200? " + (z == w)); //out of cache, false
        System.out.println("equals    : " + z.equals(w));
    }
}

Execution result

a == b      : true
a == c      : false
a.equals(c) : true
100==100? true
200==200? false
equals    : true

Note: Integer caches only -128 ~ 127, causing == to accidentally become true. Object is always .equals().

Example 4 — Bitwise.java: Bitwise operations

public class Bitwise {
    public static void main(String[] args) {
        int a = 0b1010;
        int b = 0b1100;
        System.out.printf("a & b = %d (%s)%n", a & b, Integer.toBinaryString(a & b));
        System.out.printf("a | b = %d (%s)%n", a | b, Integer.toBinaryString(a | b));
        System.out.printf("a ^ b = %d (%s)%n", a ^ b, Integer.toBinaryString(a ^ b));
        System.out.printf("~a    = %d%n", ~a);
        System.out.printf("a<<1  = %d%n", a << 1);
        System.out.printf("a>>1  = %d%n", a >> 1);
    }
}

Execution result

a & b = 8 (1000)
a | b = 14 (1110)
a ^ b = 6 (110)
~a    = -11
a<<1  = 20
a>>1  = 5

Note: ~a is -(a+1). That's because of 2's complement representation.

Common Mistakes

  1. Use == for object comparison → Replace with .equals()
  2. Calculate int a = 1/2; without knowing it is 0
  3. Mixing the postfix i++ in the if condition results in confusing results.
  4. Confusion between && and & (the former evaluates short circuit, the latter always evaluates both sides)
  5. A bug where == sometimes accidentally works because of the Integer cache.

Cleanup

  • Consciously distinguish between integer and real number operations
  • Object comparison is always .equals()
  • Bit operations are useful in special situations such as flags and hashes.
  • Short circuit evaluation is a key tool for null checking

Try it yourself

cd 01_Basics/03_Operators-and_Expressions/src
javac Arithmetic.java
java Arithmetic

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/Arithmetic.javaJava
public class Arithmetic {
    public static void main(String[] args) {
        int a = 17, b = 5;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.println(a % b);
        System.out.println((double) a / b);
    }
}
src/Bitwise.javaJava
public class Bitwise {
    public static void main(String[] args) {
        int a = 0b1010;
        int b = 0b1100;
        System.out.printf("a & b = %d (%s)%n", a & b, Integer.toBinaryString(a & b));
        System.out.printf("a | b = %d (%s)%n", a | b, Integer.toBinaryString(a | b));
        System.out.printf("a ^ b = %d (%s)%n", a ^ b, Integer.toBinaryString(a ^ b));
        System.out.printf("~a    = %d%n", ~a);
        System.out.printf("a<<1  = %d%n", a << 1);
        System.out.printf("a>>1  = %d%n", a >> 1);
    }
}
src/EqualsVsEqEq.javaJava
public class EqualsVsEqEq {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = new String("Java");
        System.out.println("a == b      : " + (a == b));
        System.out.println("a == c      : " + (a == c));
        System.out.println("a.equals(c) : " + a.equals(c));

        Integer x = 100;
        Integer y = 100;
        Integer z = 200;
        Integer w = 200;
        System.out.println("100==100? " + (x == y));
        System.out.println("200==200? " + (z == w));
        System.out.println("equals    : " + z.equals(w));
    }
}
src/LogicAndTernary.javaJava
public class LogicAndTernary {
    public static void main(String[] args) {
        int score = 75;
        boolean passed = score >= 60;
        System.out.println("Passed?" + passed);

        String grade = (score >= 90) ? "A"
                     : (score >= 80) ? "B"
                     : (score >= 70) ? "C"
                     : (score >= 60) ? "D"
                     : "F";
        System.out.println("Rating:" + grade);

        boolean ok = score > 0 && score <= 100;
        System.out.println("Effective range?" + ok);
    }
}

Assignment

Problem 1 — Determining leap years

  • File name: Homework01.java
  • Key concepts: comparison/logical operators, paragraph evaluation

Requirements

  • Substitute 2024 into variable year.
  • Write the leap year condition (multiple of 4 AND (not divisible by 100 OR multiple of 400)) in one expression.
  • Output the results in Is 2024 a leap year? true format.

Expected output

Is 2024 a leap year? true

Hint

  • (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)

Problem 2 — Grading

  • File name: Homework02.java
  • Key concept: ternary operator nesting

Requirements

  • Declare score score = 73.
  • Determine the grade (A/B/C/D/F) using only ternary operators.
  • Output in Score=73 -> Grade=C format.

Expected output

Score=73 -> Grade=C

Hint

  • (score >= 90) ? "A" : (score >= 80) ? "B" : ...

Problem 3 — Determining even or odd by bits

  • File name: Homework03.java
  • Key concept: bit AND &

Requirements

  • For integers from 1 to 5, Even/Odd is output as the result of (n & 1).

Expected output

1 -> odd number
2 -> even number
3 -> odd number
4 -> even number
5 -> odd number

Hint

  • for repetition is covered in detail in Part 04, but simple patterns can be used in advance.

Check the correct answer

After solving it yourself, compare it with the reference solution below.

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

Homework01.javaJava
/** Leap year detection.*/
public class Homework01 {
    public static void main(String[] args) {
        int year = 2024;
        boolean leap = (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
        System.out.println(year + "Is it a leap year?" + leap);
    }
}
Homework02.javaJava
/** Determination of grade using ternary operator.*/
public class Homework02 {
    public static void main(String[] args) {
        int score = 73;
        String grade = (score >= 90) ? "A"
                     : (score >= 80) ? "B"
                     : (score >= 70) ? "C"
                     : (score >= 60) ? "D"
                     : "F";
        System.out.println("score=" + score + "-> Grade =" + grade);
    }
}
Homework03.javaJava
/** Distinguish even/odd with bit AND.*/
public class Homework03 {
    public static void main(String[] args) {
        for (int n = 1; n <= 5; n++) {
            String kind = ((n & 1) == 0) ? "even number" : "odd number";
            System.out.println(n + " -> " + kind);
        }
    }
}

Learn this lesson with a mentor

Work through the Java + Spring Boot track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now