November 18, 2026•8 min read

Java boolean, char, and String: Why == Rejected a Matching Code

The voucher program prints INVA-2026 and then rejects it. == on a String asks whether two variables hold the same object, so a value built at run time never matches the literal.

Read in Indonesian

Voucher.java reads the line INVA-2026,ok, keeps the part before the comma, and prints it. Then it checks that code against INVA-2026. Run it and code: INVA-2026 appears on screen, followed by verdict: not valid.

JAVA
// Voucher.java
public class Voucher {
    public static void main(String[] args) {
        String line = "INVA-2026,ok";
        String code = line.split(",")[0];
        System.out.println("code: " + code);
        if (code == "INVA-2026") {
            System.out.println("verdict: valid");
        } else {
            System.out.println("verdict: not valid");
        }
    }
}
Output
$ javac Voucher.java && java Voucher
code: INVA-2026
verdict: not valid

The bug is one == in the if, and understanding it requires three types: boolean, char, and String. The voucher hits the String comparison trap.

Key takeaways

  • == on a String asks whether two variables hold the same object; .equals() asks whether two texts are the same.
  • A String built at run time, by split or by new String, is never the same object as the literal it matches.
  • boolean takes only true and false, and Java refuses to convert a number into it.
  • if (passed = true) is an assignment that compiles and is always true.
  • char is a 16-bit number in quotes: 'a' + 'b' is 195, and '7' - '0' is 7.
  • A checker that compares the program's output with the expected answer catches the wrong verdict that the run itself reports as fine.

Two values, no numbers

A boolean holds true or false and nothing else, and Java will not convert a number into one for you, so this does not compile:

JAVA
// BadBoolean.java
public class BadBoolean {
    public static void main(String[] args) {
        boolean passed = 1;
        System.out.println(passed);
    }
}
Output
$ javac BadBoolean.java
BadBoolean.java:4: error: incompatible types: int cannot be converted to boolean
        boolean passed = 1;
                         ^
1 error

Java does not treat 1 as true; you write true.

The if statement needs a boolean between its parentheses, and that opens a second trap. A single = stores a value while a double == compares two values, so type one = by mistake and Java still accepts it:

JAVA
// AssignmentInIf.java
public class AssignmentInIf {
    public static void main(String[] args) {
        boolean passed = false;
        if (passed = true) {
            System.out.println("branch taken: " + passed);
        }
        System.out.println("after: " + passed);
    }
}
Output
$ javac AssignmentInIf.java && java AssignmentInIf
branch taken: true
after: true

The variable started as false, the branch ran anyway, and passed is true afterwards. passed = true is an assignment rather than a question, so the condition is always true and the compiler accepts it without warning. It will not always tell you which operator you meant.

char is a 16-bit integer

A char holds one character, written in single quotes like 'a', and underneath it is a number. It is 16 bits wide and holds one UTF-16 code unit: Character.SIZE is 16, Character.BYTES is 2, and the largest value is 65535.

Because a char is a number, arithmetic works on it: 'a' + 'b' is 195, and the result is an int rather than a letter.

Adding to a constant still fits back into a char, so char next = 'a' + 1; compiles and prints b. Do the same with a variable and Java refuses:

JAVA
// BadChar.java
public class BadChar {
    public static void main(String[] args) {
        char ch = 'a';
        char next = ch + 1;
        System.out.println(next);
    }
}
Output
$ javac BadChar.java
BadChar.java:5: error: incompatible types: possible lossy conversion from int to char
        char next = ch + 1;
                       ^
1 error

The expression ch + 1 is an int, and an int may not fit in a char, which is why the compiler says "possible lossy conversion".

Subtracting '0' from a digit character gives you the number it shows, so '7' - '0' is 7. Use it whenever you hold a digit as a char and want its value.

A String is not the text

A String holds text, but the variable does not contain the text itself: it points to a String object, a thing you can ask questions with a dot, like s.length().

A String never changes after it is created, so s.toUpperCase() hands back a new one: with s set to inva, the variable upper becomes INVA and s stays inva.

length() counts UTF-16 code units, the same unit a char holds, so an emoji written as "\uD83D\uDE00" has a length of 2 while codePointCount reports 1 character.

Now back to the voucher. == asks whether these are the same object, .equals() asks whether this is the same text, and Types.java runs five comparisons against INVA-2026:

JAVA
// Types.java
public class Types {
    public static void main(String[] args) {
        System.out.println("Character.SIZE = " + Character.SIZE);
        System.out.println("Character.BYTES = " + Character.BYTES);
        System.out.println("(int) Character.MAX_VALUE = " + (int) Character.MAX_VALUE);
        System.out.println("'a' + 'b' = " + ('a' + 'b'));
        char next = 'a' + 1;
        System.out.println("char next = 'a' + 1 -> " + next);
        System.out.println("'7' - '0' = " + ('7' - '0'));

        String s = "inva";
        String upper = s.toUpperCase();
        System.out.println("s = " + s + ", upper = " + upper);

        String literal = "INVA-2026";
        String built = "INVA-" + 2026;
        String made = new String("INVA-2026");
        String fromSplit = "INVA-2026,ok".split(",")[0];
        System.out.println("literal == built     : " + (literal == built));
        System.out.println("literal == made      : " + (literal == made));
        System.out.println("literal == fromSplit : " + (literal == fromSplit));
        System.out.println("literal == made.intern() : " + (literal == made.intern()));
        System.out.println("literal.equals(made) : " + literal.equals(made));

        String emoji = "\uD83D\uDE00";
        System.out.println("emoji.length() = " + emoji.length());
        System.out.println("emoji.codePointCount = " + emoji.codePointCount(0, emoji.length()));
    }
}
Output
$ javac Types.java && java Types
Character.SIZE = 16
Character.BYTES = 2
(int) Character.MAX_VALUE = 65535
'a' + 'b' = 195
char next = 'a' + 1 -> b
'7' - '0' = 7
s = inva, upper = INVA
literal == built     : true
literal == made      : false
literal == fromSplit : false
literal == made.intern() : true
literal.equals(made) : true
emoji.length() = 2
emoji.codePointCount = 1

Read the five results. literal == built is true, because both are compile-time constants. literal == made is false, since new String(...) creates a new object. literal == fromSplit is false too. Calling made.intern() brings back the shared object, so that comparison is true. literal.equals(made) is true: the text is the same.

The voucher's code came out of split(",")[0] just like fromSplit, so it is not the same object as the literal and == said no.

A check you can run

A program can find this bug for you. CheckVoucher reads lines, compares each one to the line it expects with .equals(), and prints PASS or FAIL, exiting with status 1 if any check failed. The pipe symbol | sends the output of Voucher into it.

JAVA
// CheckVoucher.java
import java.util.List;
import java.util.Scanner;

public class CheckVoucher {
    public static void main(String[] args) {
        List<String> expected = List.of("code: INVA-2026", "verdict: valid");
        Scanner in = new Scanner(System.in);
        int passed = 0;
        for (int i = 0; i < expected.size(); i++) {
            String got = in.hasNextLine() ? in.nextLine() : "<nothing>";
            if (got.equals(expected.get(i))) {
                passed++;
                System.out.println("PASS  line " + (i + 1) + ": " + got);
            } else {
                System.out.println("FAIL  line " + (i + 1) + ": expected " + expected.get(i) + ", got " + got);
            }
        }
        if (in.hasNextLine()) {
            System.out.println("FAIL  extra line: " + in.nextLine());
        }
        System.out.println();
        System.out.println(passed + " of " + expected.size() + " checks passed");
        if (passed != expected.size()) {
            System.exit(1);
        }
    }
}
Output
$ javac CheckVoucher.java && java Voucher | java CheckVoucher
PASS  line 1: code: INVA-2026
FAIL  line 2: expected verdict: valid, got verdict: not valid

1 of 2 checks passed

Line 1 passes, line 2 fails, and the run ends with exit status 1.

Ask about the text, not the object

The fix is one line: replace == with .equals().

JAVA
// Voucher.java
public class Voucher {
    public static void main(String[] args) {
        String line = "INVA-2026,ok";
        String code = line.split(",")[0];
        System.out.println("code: " + code);
        if (code.equals("INVA-2026")) {
            System.out.println("verdict: valid");
        } else {
            System.out.println("verdict: not valid");
        }
    }
}

Run the same checker again:

Output
$ javac Voucher.java && java Voucher | java CheckVoucher
PASS  line 1: code: INVA-2026
PASS  line 2: verdict: valid

2 of 2 checks passed

Both lines pass, the count reads 2 of 2 checks passed, and the exit status is 0.

The checker is a second reader

In Voucher.java, change the code the program reads to INVA-2027 and leave the comparison at INVA-2026. Run the same command.

JAVA
// Voucher.java
public class Voucher {
    public static void main(String[] args) {
        String line = "INVA-2027,ok";
        String code = line.split(",")[0];
        System.out.println("code: " + code);
        if (code.equals("INVA-2026")) {
            System.out.println("verdict: valid");
        } else {
            System.out.println("verdict: not valid");
        }
    }
}
Output
$ javac Voucher.java && java Voucher | java CheckVoucher
FAIL  line 1: expected code: INVA-2026, got code: INVA-2027
FAIL  line 2: expected verdict: valid, got verdict: not valid

0 of 2 checks passed

Both lines fail and the exit status is 1, because the checker still expects INVA-2026 and reports the difference on line 1.

Compare strings with .equals(), and use a small checker to verify the output rather than scanning it by eye.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
Struktur DataWhat a Linked List Actually Is

A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.

PascalPascal Capstone: Why Every Gradebook Score Came Back Zero

A gradebook reads three students from a file, prints three rows, and every score is 0. The procedure filled a copy of the array, because the parameter was missing var. Add the word and the report matches the file; the compiler had already warned about the call that filled nothing.

For developers who learn by struggling

© 2026 Inva.dev.