473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

another bit of trickery-dickery

11,448 Expert 8TB
Since it's so quiet overhere: what will happen when this snippet is executed?

Expand|Select|Wrap|Line Numbers
  1. String s = "000A003B";
  2. if (true == false) // "s" will be changed later to \u000A\u003B
  3.    System.out.println("boo");
  4.  
I just read this one somewhere else and I like it ;-)

kind regards,

Jos
May 15 '08 #1
12 1416
Laharl
849 Expert 512MB
I'm assuming there's more to this snippet, having just executed it and had nothing print, as expected.
May 15 '08 #2
BigDaddyLH
1,216 Expert 1GB
Since it's so quiet overhere: what will happen when this snippet is executed?

Expand|Select|Wrap|Line Numbers
  1. String s = "000A003B";
  2. if (true == false) // "s" will be changed later to \u000A\u003B
  3.    System.out.println("boo");
  4.  
I just read this one somewhere else and I like it ;-)

kind regards,

Jos
My compiler warned me :-(
May 15 '08 #3
BigDaddyLH
1,216 Expert 1GB
Here's another, copied from Java Puzzlers:
Expand|Select|Wrap|Line Numbers
  1. public class Outer {
  2.   class Inner1 extends Outer {}
  3.   class Inner2 extends Inner1 {}
  4. }
That won’t compile. Why not? Explain. How can you fix it?
May 15 '08 #4
Laharl
849 Expert 512MB
Inner classes need extra syntax, if my memory serves, something involving };.
May 15 '08 #5
BigDaddyLH
1,216 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. ClassName x = new ClassName() {
  2.     public void f() {}
  3. };
and there the "};" syntax is really a special case of
Expand|Select|Wrap|Line Numbers
  1. ClassName x = expression ;//<<--
May 15 '08 #6
JosAH
11,448 Expert 8TB
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 16 '08 #7
JosAH
11,448 Expert 8TB
Here's another, copied from Java Puzzlers:
Expand|Select|Wrap|Line Numbers
  1. public class Outer {
  2.   class Inner1 extends Outer {}
  3.   class Inner2 extends Inner1 {}
  4. }
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 16 '08 #8
BigDaddyLH
1,216 Expert 1GB
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 16 '08 #9
JosAH
11,448 Expert 8TB
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
May 16 '08 #10
Laharl
849 Expert 512MB
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 16 '08 #11
sukatoa
539 512MB
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 17 '08 #12
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. // blahblah \u000a\u003b
  2.  
is translated as:

Expand|Select|Wrap|Line Numbers
  1. // blahblah
  2. ;
  3.  
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
May 17 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
3
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.....
17
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...
2
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...
8
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.