There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Copy the code into your favorite IDE
Tim Driven Development | Test until the fear goes away
That would very unexpected to me too, if I wouldn't ever be instructed by Campbell Ritchie to read Java Puzzlers by Joshua Bloch. Jochua calls this puzzle as Line Printer. But I'll leave that for someone else to enjoy and figure out. Nice oneRoel De Nijs wrote:Who can explain this unexpected result?
Piet Souris wrote:Copy the code into your favorite IDE, then click on the right '{' from the lineso that it will be hilighted. Then look at where its counterpart is hilighted.
Roel De Nijs wrote:
Piet Souris wrote:Copy the code into your favorite IDE
That's cheating! (...)
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Oh no! Was it not you who stated " If you compile and execute this code snippet (...)"?
Well, I always use an IDE for that!
Piet Souris wrote:But in all sincerity: is that an exam question (or could such a question be asked)?
If so, what exactly are they testing here?
In Java code you can use Unicode escape sequences as illustrated by the comment on line3. Unicode escape sequences (like \u000a) are replaced by the actual characters they represent before the Java compiler does anything else with the source code. So the comment \u000a\u007d\u007b has 3 Unicode escape sequences and thos are replaced internally with the following characters:
\u000a: line feed
\u007d: right (closing) curly bracket }
\u007b: left (opening) curly bracket {
That appears to be on page 35.Liutauras Vilda wrote:. . . Java Puzzlers by Joshua Bloch. Jochua calls this puzzle as Line Printer. . . . .
Campbell Ritchie wrote:I am not quite sure you should call that a comment, Roel. The comment consists of a space or two; because as you say the \u000a is assessed early in the compilation process, the {} moves to the next line (sorry }{ not {}) and does not constitute part of the comment.
Campbell Ritchie wrote:And why have you got a space after \u0073 in line 17?
And if that isn't a good puzzler, I don't know what is!Roel De Nijs wrote:. . . it definitely looks like a comment. . . .
salvin francis wrote:
Tim Driven Development | Test until the fear goes away
Ramya Subraamanian wrote:I have a partial answer. Not sure how this unicode "// File: code\com\united\Airplane.java" tweaks the output.
Ramya Subraamanian wrote:line 7 must print first row - I
line 8 - space
line 9 second row - am
line 10 - flying.space
line 11 third row- whiiiiii!
And in fact, it might even be a question on the OCA exam as you need to be able to identify valid and invalid Unicode escape sequences.
Ramya Subraamanian wrote:its strange.. if you remove "com" from this line. it compiles fine.
JLS wrote:3.3
(...)
In addition to the processing implied by the grammar, for each raw input character that is a backslash \, input processing must consider how many other \ characters contiguously precede it, separating it from a non-\ character or the start of the input stream. If this number is even, then the \ is eligible to begin a Unicode escape; if the number is odd, then the \ is not eligible to begin a Unicode escape.
Ramya Subraamanian wrote:I was not aware of this.but i don't see them in the exam topics at the oracle site.