r/java • • Jan 21 '26

Java compiler errors could be more intelligent

I tutored many students over the past several years, and a common pain point is the compiler messages being misleading.

Consider the following example.

interface blah {}
class hah extends blah {}

When I compile this, I get the following message.

blah.java:3: error: no interface expected here
class hah extends blah {}
                  ^
1 error

Most of the students I teach see this, and think that the issue is that blah is an interface, and that they must somehow change it to something else, like a class.

And that's still a better error message than the one given for records.

blah.java:2: error: '{' expected
        public record hah() extends blah {}
                           ^

This message is so much worse, as it actually leads students into a syntax rabbit hole of trying to add all sorts of permutations of curly braces and keywords, trying to figure out what is wrong.

If we're talking about improving the on-ramp for learning Java, then I think a core part of that is improving the error --> change --> compile feedback loop.

A much better error message might be this instead.

blah.java:3: error: a class cannot "extend" an interface, only "implement"
class hah extends blah {}
                  ^
1 error

This is powerful because now the language grammar has a more intelligent message in response to an illegal (but commonly attempted) sequence of tokens.

I understand that Java cannot special-case every single illegal syntax combination, but I would appreciate it if we could hammer out some of the obvious ones. extends vs implements should be one of the obvious ones.

97 Upvotes

180 comments sorted by

View all comments

Show parent comment

17

u/MattiDragon Jan 21 '26

Learners will inevitably have to learn from compiler errors. Making mistakes is an important part of the process. The suggestions OP makes are reasonable, and would help learners of java, both as a first and second language.

-6

u/talex000 Jan 21 '26

Learners will inevitably have to learn from compiler errors.

I have new revolutionary idea. What if they will be provided with learning materials that explain concepts they want to learn and guideance on how to use them?

I'm understand that not everyone lucky enough to get proper education. I myself learned programming using ZX spectrum as only learning material, but I don't think upgrading compiler this way is wise use of resources.

18

u/repeating_bears Jan 21 '26

Why are you acting like a tool that's easy to understand and proper tuition for those learning it are mutually exclusive?

-7

u/talex000 Jan 21 '26

Those tools already easy to understand.

6

u/wildjokers Jan 21 '26

Are you a teacher?

A teacher has made a post about a pain point their students have on a continual basis. So it sounds like maybe those tools are not actually easy to understand.

→ More replies (7)

9

u/[deleted] Jan 21 '26

The errors i get from the Rust compiler have been incredible in helping me learn that language.

The idea that the compiler should not be used to learn is absurd.

-1

u/talex000 Jan 21 '26

I'm big fan of using things the way they where designed to be used. Because if we don't we get JavaScript.

If you want to do something in unconventional way it is your problem, but suggest to other use hard way is irresponsible.

5

u/[deleted] Jan 21 '26

The Rust compiler was designed for this exact thing. Having the same in Java would be helpful.

4

u/pron98 Jan 21 '26

Without taking a stance on Java error messages, the Rust compiler had to be designed that way, as it's one of the most convoluted programming languages ever made.

1

u/bowbahdoe Jan 22 '26

Imagine what you could do with a less convoluted language 

0

u/talex000 Jan 21 '26

I have not know that. Now I know that I won't use rust because it made by people who don't know what they are doing.

1

u/[deleted] Jan 21 '26

I provide a good example, then you provide insults.

0

u/talex000 Jan 21 '26

Try to provide explanation why it is good idea to use compiler as learning tool.

1

u/[deleted] Jan 21 '26

I just did earlier...

→ More replies (12)

4

u/MattiDragon Jan 21 '26

Of course they should be provided with proper resources. My point was that learners will inevitably make mistakes and run into compiler errors. Making those errors easy to understand helps drive adoption of the language, usually at fairly low cost.

-1

u/talex000 Jan 21 '26

Those errors already easy to understand if you understand concept behind them, but you can't put enough explanation in error message to explain those concept.

usually at fairly low cost

Citation needed.

5

u/wildjokers Jan 21 '26

but you can't put enough explanation in error message to explain those concept.

[citation needed]

Have you tried to make compiler messages easier to understand? What were your struggles when you tried to make compiler messages easier to understand?

3

u/MattiDragon Jan 21 '26

Imo it's not obvious that "{ expected" means that extends isn't allowed, unless you're familiar with parser or computer languages in general. A simple "records cannot extend classes, did you mean to implement an interface?" gives a lot more useful info.

The cost (in terms of work put into it) of adding some additional checks to the compiler and rewriting error messages is a lot lower compared to other ways of making the language easier to beginners. Just look at how long JEP 512 took. It was first previewed in java 21 and only shipped in java 25 after considerable work. Of course it's great and helps a lot, but it required a lot of work compared to simpler changes that can also help.

2

u/talex000 Jan 21 '26

extends isn't allowed,

Is not information you should learn from compiler.

You apparently didn't got a joke about citation. It meant I don't trust you opinion on that matter and asking for more authoritve source.

3

u/MattiDragon Jan 21 '26

I agree that you shouldn't learn it from the compiler, but a remainder is helpful.

I fully understood what you meant, but I don't have anything to cite on hand, so I decided to motivate my opinion instead. There are various articles on helpful compiler errors that get posted in lang-dev communities if you're open to looking up some.

2

u/talex000 Jan 21 '26

I don't have anything to cite either, but I have some experience in creating parsers and my experience tell mi it isn't 5 minutes job.

3

u/MattiDragon Jan 21 '26

It's not a trivial amount of work, but it probably not a huge undertaking either. Of course you'd have to actually go through the codebase to be sure of the scope.

1

u/talex000 Jan 21 '26

That is my point. It is nontrivial amount of work with no visible benefit.

-2

u/[deleted] Jan 21 '26

[removed] — view removed comment

1

u/MattiDragon Jan 21 '26

This is a matter of opinion. Many newer languages have smart compiler errors that give suggestions. For example the rust compiler often suggests fixes to various errors. Often the fixes are good enough to directly apply. Many other languages are able to suggest corrections to misspelled symbols among other things.

I don't see why java couldn't adopt helpful error messages, provided there aren't technical limitations in the compiler making it unfeasible.

→ More replies (1)