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
12 1416
I'm assuming there's more to this snippet, having just executed it and had nothing print, as expected.
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 :-(
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?
Inner classes need extra syntax, if my memory serves, something involving };.
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 ;//<<--
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
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
Here's Joshua Bloch's and Neal Gafter's explanation (it's puzzle #9): http://www.javapuzzlers.com/java-puzzlers-sampler.pdf
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
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...
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....
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)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Go USA! Go Israel! |
last post by:
I was wondering if the following was possible:
Instead of using a frameset which references other individual HTML files,
have the <SPAN> tag reference another HTML file, which is placed within...
|
by: teranews |
last post by:
My question is this...
Is 'LINK'ing a stylesheet required before 'IMPORT'ing another for successful
hovering?
I have a problem which cropped up with the introduction of IE 7 Beta 2...
Yes.....
|
by: Rabbit |
last post by:
Hi,
On my 1st page, i have a function which gets a new ID value and need to
transfer to another immediately.
which I want to get in 2nd page using Request.form("txtID"), but doesn't
work, the...
|
by: John Salerno |
last post by:
Ah, the object-oriented stuff is just so FUN! :) Here's my code,
followed by the error. I thought I was referring to the 'text' attribute
correctly, but it seems not.
import wx
class...
|
by: anon.asdf |
last post by:
Hi!
OK, lets try "array-copy":
{
char arrayA;
arrayA = (char){1, 2, 3};
}
it does *not* work since we're trying to make a fixed array-pointer
arrayA, point to another location/address...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |