another bit of trickery-dickery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    another bit of trickery-dickery

    Since it's so quiet overhere: what will happen when this snippet is executed?

    [code=java]
    String s = "000A003B";
    if (true == false) // "s" will be changed later to \u000A\u003B
    System.out.prin tln("boo");
    [/code]
    I just read this one somewhere else and I like it ;-)

    kind regards,

    Jos
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    I'm assuming there's more to this snippet, having just executed it and had nothing print, as expected.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by JosAH
      Since it's so quiet overhere: what will happen when this snippet is executed?

      [code=java]
      String s = "000A003B";
      if (true == false) // "s" will be changed later to \u000A\u003B
      System.out.prin tln("boo");
      [/code]
      I just read this one somewhere else and I like it ;-)

      kind regards,

      Jos
      My compiler warned me :-(

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Here's another, copied from Java Puzzlers:
        [CODE=Java]public class Outer {
        class Inner1 extends Outer {}
        class Inner2 extends Inner1 {}
        }[/CODE]
        That won’t compile. Why not? Explain. How can you fix it?

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Inner classes need extra syntax, if my memory serves, something involving };.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Laharl
            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:

            [CODE=Java]ClassName x = new ClassName() {
            public void f() {}
            };[/CODE]

            and there the "};" syntax is really a special case of
            [CODE=Java]ClassName x = expression ;//<<--[/CODE]

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Laharl
              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

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by BigDaddyLH
                Here's another, copied from Java Puzzlers:
                [CODE=Java]public class Outer {
                class Inner1 extends Outer {}
                class Inner2 extends Inner1 {}
                }[/CODE]
                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

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by JosAH
                  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):

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by BigDaddyLH
                    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

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      Originally posted by JosAH
                      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...

                      Comment

                      • sukatoa
                        Contributor
                        • Nov 2007
                        • 539

                        #12
                        Originally posted by Laharl
                        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("000A003 B"), 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.prin tln("boo");
                        }

                        boo doesn't print....

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by sukatoa
                          Me too... no warnings....
                          Whenever i swap those boolean literals, nothings happen(boo printed)....
                          i've changed "000A003B" to new String("000A003 B"), 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.prin tln("boo");
                          }

                          boo doesn't print....
                          You're very close; the answer from the JLS (Java Language Specification)

                          Originally posted by JLS
                          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:

                          [code=java]
                          // blahblah \u000a\u003b
                          [/code]

                          is translated as:

                          [code=java]
                          // blahblah
                          ;
                          [/code]

                          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

                          Comment

                          Working...