another bit of trickery-dickery
Question posted by: JosAH
( Chief Editor)
on
May 15th, 2008 06:59 PM
Since it's so quiet overhere: what will happen when this snippet is executed?
-
String s = "000A003B";
-
if (true == false) // "s" will be changed later to \u000A\u003B
-
System.out.println("boo");
I just read this one somewhere else and I like it ;-)
kind regards,
Jos
|
|
May 15th, 2008 08:38 PM
# 2
|
Re: another bit of trickery-dickery
I'm assuming there's more to this snippet, having just executed it and had nothing print, as expected.
|
|
May 15th, 2008 08:44 PM
# 3
|
Re: another bit of trickery-dickery
Quote:
Since it's so quiet overhere: what will happen when this snippet is executed?
-
String s = "000A003B";
-
if (true == false) // "s" will be changed later to \u000A\u003B
-
System.out.println("boo");
I just read this one somewhere else and I like it ;-)
kind regards,
Jos
|
My compiler warned me :-(
|
|
May 15th, 2008 09:15 PM
# 4
|
Re: another bit of trickery-dickery
Here's another, copied from Java Puzzlers:
- public class Outer {
-
class Inner1 extends Outer {}
-
class Inner2 extends Inner1 {}
-
}
That won’t compile. Why not? Explain. How can you fix it?
|
|
May 15th, 2008 09:37 PM
# 5
|
Re: another bit of trickery-dickery
Inner classes need extra syntax, if my memory serves, something involving };.
|
|
May 15th, 2008 09:45 PM
# 6
|
Re: another bit of trickery-dickery
Quote:
Inner classes need extra syntax, if my memory serves, something involving };.
|
Do you mean "};" -- a semicolon after a certain close-brace? No, you are thinking of anonymous class syntax:
- ClassName x = new ClassName() {
-
public void f() {}
-
};
and there the "};" syntax is really a special case of
- ClassName x = expression ;//<<--
|
|
May 16th, 2008 06:40 AM
# 7
|
Re: another bit of trickery-dickery
Quote:
I'm assuming there's more to this snippet, having just executed it and had nothing print, as expected.
|
Your compiler must be broken then.
kind regards,
Jos
|
|
May 16th, 2008 06:48 AM
# 8
|
Re: another bit of trickery-dickery
Quote:
Here's another, copied from Java Puzzlers:
- public class Outer {
-
class Inner1 extends Outer {}
-
class Inner2 extends Inner1 {}
-
}
That won’t compile. Why not? Explain. How can you fix it?
|
If you make the Inner1 class a static class it compiles but it ruins the intended
class structure ;-)
kind regards,
Jos
|
|
May 16th, 2008 05:59 PM
# 9
|
Re: another bit of trickery-dickery
Quote:
If you make the Inner1 class a static class it compiles but it ruins the intended
class structure ;-)
kind regards,
Jos
|
Here's Joshua Bloch's and Neal Gafter's explanation (it's puzzle #9):
http://www.javapuzzlers.com/java-puzzlers-sampler.pdf
|
|
May 16th, 2008 07:02 PM
# 10
|
Re: another bit of trickery-dickery
I understand; my 'solution' just got rid of the entire problem by not needing that
sort of unknown Outer instantiation needed by that Inner2 constructor. For now
I find their solution a bit of a language deficiency but I can't be a discussion
partner in that issue yet: I find it filthy ;-)
Ok, now what about the riddle I transmogrified from Sun's forum to this one?
No (correct) answers yet ;-)
kind regards,
Jos
|
|
May 16th, 2008 10:06 PM
# 11
|
Re: another bit of trickery-dickery
Quote:
Your compiler must be broken then.
kind regards,
Jos
|
I tried it again and...there are indeed shenanigans involved. Not only did the expected 'unreachable statement' compiler warning not appear, but 'boo' was printed. No clue why, though...
|
|
May 17th, 2008 09:02 AM
# 12
|
Re: another bit of trickery-dickery
Quote:
I tried it again and...there are indeed shenanigans involved. Not only did the expected 'unreachable statement' compiler warning not appear, but 'boo' was printed. No clue why, though...
|
Me too... no warnings....
Whenever i swap those boolean literals, nothings happen(boo printed)....
i've changed "000A003B" to new String("000A003B"), nothings changed....
i changed the comment, boo not printed... i removed \u000A\u003B, boo not printed....
So, is \u000A\u003B is not really part of the comment? since they are line feed and a semicolon, is \u000A\u003B are acceptable statement in java?..
because, when i put a brace inside of it
if(true==false){// "s" will be changed later to \u000A\u003B
System.out.println("boo");
}
boo doesn't print....
|
|
May 17th, 2008 09:40 AM
# 13
|
Re: another bit of trickery-dickery
Quote:
Me too... no warnings....
Whenever i swap those boolean literals, nothings happen(boo printed)....
i've changed "000A003B" to new String("000A003B"), nothings changed....
i changed the comment, boo not printed... i removed \u000A\u003B, boo not printed....
So, is \u000A\u003B is not really part of the comment? since they are line feed and a semicolon, is \u000A\u003B are acceptable statement in java?..
because, when i put a brace inside of it
if(true==false){// "s" will be changed later to \u000A\u003B
System.out.println("boo");
}
boo doesn't print....
|
You're very close; the answer from the JLS (Java Language Specification)
Quote:
3.2 Lexical Translations
A raw Unicode character stream is translated into a sequence of tokens, using the following three lexical translation steps, which are applied in turn:
A translation of Unicode escapes (§3.3) in the raw stream of Unicode characters to the corresponding Unicode character. A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.
|
So first the raw input stream is analyzed and any \uxxxx sequence of characters
(xxxx == four hexadecimal nibbles) is translated to its Unicode character. So
the code:
is translated as:
Note that \u000a is translated to a newline character and \u003b is translated to
a semi colon. The semicolon effectively ends the nonsensical if statement in the
original example and that's why "boo" is printed (the entire print statement is
not part of the if-statement anymore although to human readers it looks like it is).
kind regards,
Jos
 |
Not the answer you were looking for? Post your question . . .
189,173 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Latest Articles: Read & Comment
Top Java Forum Contributors
|