← Back to list

Java ignores whitespace — so why did ‘intx’ crash my program?

Even though whitespace feels like wasted effort — and the compiler supposedly ignores it — isn’t it unfair that we still have to type it?

Java-learner · 2026-04-27 23:09 · 0 claps · 2.7 min read
#beginner-java #java-beginners-tutorial #java-tips #java-basics #learn-java-basics
Open on Medium ↗
Wiki topics: 🔭 · Astronomy & Space

Java ignores whitespace — so why did ‘intx’ crash my program?

Photo by Brett Jordan on Unsplash

Photo by Brett Jordan on Unsplash

Even though whitespace feels like wasted effort — and the compiler supposedly ignores it — isn’t it unfair that we still have to type it?

It’s often said that Java ignores whitespace. That’s true… until it suddenly isn’t. When a single missing space can crash your program, it’s time to look deeper.

Let’s start with a simple test.

The “canyoureadnow” test

First, whitespace is for us humans. Look at this:

canyoureadnow

Probably not. Now try:

can you read now

Better, right? Whitespace makes text readable. Same with code.

The experiment that failed

I wrote simple Java program:

public class JavaVariable {
       public static void main(String[] args) {
            int x = 3 + 4;
            System.out.println(x);
      }
}

It worked fine. Then I thought: Why do I need that space? So, I removed it:

intx = 3 + 4;    // After removing the space, int x becomes intx – a single identifier 
                // The keyword int is gone.
System.out.println(x);

Java refused to compile:

JavaVariable.java:3: error: cannot find symbol
    intx = 3 + 4;
    ^
  symbol:   variable intx

Wait — I declared intx, didn’t I? The code adds two numbers. What’s the problem?

What the compiler actually does

The compiler doesn’t read intentions. It reads rules. Specifically, it follows maximal munch: always take the longest possible valid token.

The compiler reads your code left to right and tries to take the longest possible valid token at each step. This rule is called **maximal munch**.

  • i → starts an identifier/keyword
  • n → still valid
  • t → still valid
  • x → still an identifier character

It consumes the whole intx as one token – an identifier named intx.

Then it sees =, 3, +, 4, ;. No problem there.

But when it looks for a variable called intx… it doesn’t exist. And x? Never declared.

If I had added a spaceint x = ... – then int would be a keyword token, x an identifier, and everything works.

The compiler doesn’t read intentions. It follows rules.

So, whitespace is not ignored — it’s a separator

The compiler doesn’t ignore whitespace. It uses it as a delimiter — a signal that one token ends and another begins.

  • int x → two tokens: keyword int, identifier x
  • intx → one token: identifier intx

One space changes everything. But if whitespace is just a separator, why do we add so much more of it? For readability…

What about readability?

Once you understand the compiler’s rules, you can add extra whitespace — indentation, blank lines, spaces around operators — to make the code readable for humans. The compiler ignores that extra space. But your teammates won’t.

But the minimum required whitespace is small.

public class Readable { 
    public static void main(String[] args) { 
        int sum = 10 + 20; // spaces make it clear 
    } 
}

That’s not for the machine. That’s for the poor soul (maybe future you) who has to read this code at 2 AM.

The lesson

Whitespace isn’t just for readability. It’s a grammatical rule of the language. Skip it, and the compiler will happily misinterpret your code.

The fix for my mistake? One space:

int x = 3 + 4;   // space between int and x – works perfectly
int x=3+4;       // even this one works as well. Why?

Operators like =, +, and ; are their own tokens, so they don’t need surrounding whitespace. Only keywords and identifiers need separation – which is why int x requires a space, but x=3 does not.

Key takeaway:

Whitespace is a token separator, not just decoration. One missing space can turn a keyword into an identifier.

So next time you press the space bar, know that you’re not wasting effort. You’re speaking the compiler’s language. And you’re being kind to the next reader.

Try this yourself: remove a single space between a keyword and an identifier. What error do you get?


메타데이터
post_id
fff4fd9564fb
slug
java-ignores-whitespace-so-why-did-intx-crash-my-program-fff4fd9564fb
url
https://medium.com/@javalearner653/java-ignores-whitespace-so-why-did-intx-crash-my-program-fff4fd9564fb
canonical_url
https://medium.com/@javalearner653/java-ignores-whitespace-so-why-did-intx-crash-my-program-fff4fd9564fb
author_url
https://medium.com/@javalearner653
status
ok
fetched_at
2026-07-11 06:52:25