• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Compiler error handling (OCP Java SE 11 Study Guide, Boyarsky/Selikoff, Ch 9)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 thre‍ad title
 
Tim Havinga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic