A question about review question 10 of chapter 9 of the book, containing the following code:
When I enter the code in the question and try to compile it, I get the following error messages:
While the answer states that line 13 is incorrect. Indeed, when I insert the semicolon at line 6, the error is:
So the correct answers would be C and D. But that is crazy. Do you know how the compiler handles these situations? I would assume that it would go forward as if the semicolon was inserted at line 6 and give the last error.
Thee is indeed a semicolon missing from what you posted as No 6. But when you corrected that, you had two incompatible versions of the same method. Please tell us which book, authors and all, and tell us the answers and then explain why you think there is something wrong.with them.
Tim Havinga wrote:Do you know how the compiler handles these situations? I would assume that it would go forward as if the semicolon was inserted at line 6 and give the last error.
You shouldn't assume that. When dealing with multiple errors, the only reliable one is the first one. If you fix that first one, some of the other ones that follow could go away.
A compiler for a non-trivial language like Java has to deal with a complex syntax tree, a tree that has many possible branches. Making the kind of assumption you mentioned could be impractical from a compiler design perspective. If you ever try to write your own compiler someday, you'll understand the issues better. Even an exercise in parsing a "simple" language like Reverse Polish notation for arithmetic expressions can be informative.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
Junilu Lacar wrote:. . . a complex syntax tree . . .
Spending time writing my own compiler and trying to incorporate error messages gave me a respect for people who write that sort of message. It is really difficult.
What you have is two methods with override‑equivalent signatures, as described in the JLS (=Java® Language Specification). You are inheriting one method from one interface and the other method from the other interface. The two methods are not “return‑type‑substitutable” for each other, so they cannot compile.
Is that code exactly what was printed in the book? If you post something even slightly different, it can cause confusion.
And thank you for adding moe details of the book to the thread title
posted 1 month ago
Thank you for your responses. I understand that creating a compiler is hard (and have not tried it myself).
The possible answers in the book (mentioned in the thread title) are:
A) It compiles without issue
B) The code will not compile because of line 5.
C) The code will not compile because of line 6.
D) The code will not compile because of line 7.
E) The code will not compile because of line 9.
F) The code will not compile because of line 10.
G) The code will not compile because of line 13.
The book states that answers C and G are correct because of the missing semicolon and the conflicting interfaces.
That had me wondering: if a line with a method declaration (line 6) fails to compile, is it then taken into account for the rest of the compilation or is it left out? In the latter case, line 13 would not issue a compiler error.
The code in my first post is identical to the question (I replaced "distance" with "dist", but that does not make a difference).
posted 1 month ago
Tim Havinga wrote:Thank you
That's a pleasure
. . . if a line with a method declaration (line 6) fails to compile, is it then taken into account for the rest of the compilation or is it left out? . . .
Don't know. It is unpredictable.
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!