• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is true really equal to false?

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

After studying really hard for a whole week to prepare for the OCA or OCP certification exam, it's definitely time to relax a bit and have some (Java) fun!

If you compile and execute this code snippet, "true is false!" will be printed to the console Who can explain this unexpected result?

Have fun!
Kind regards,
Roel
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Copy the code into your favorite IDE, then click on the right '{'
from the line

so that it will be hilighted. Then look at where its counterpart
is hilighted.

Surprising! I would have thought that a comment is a comment!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Copy the code into your favorite IDE


That's cheating! You should not use an IDE when preparing/studying for the certification exam as you are not allowed to use one on the actual exam
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh that's a good one. I like it
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Who can explain this unexpected result?

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 one

[edit] by the way, example slightly different there, but similarities you could find too. At least it would look suspicious.

Have to admit, I would be tricked by that one too.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


I tried this one in my IDE (Eclipse Kepler SR2) and it highlights the '}' on line6, so that looks pretty normal to me And when I do the same with '{' from the linethe '}' on line5 is highlighted (as you would expect as well).
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Piet Souris wrote:Copy the code into your favorite IDE


That's cheating! (...)



Oh no! Was it not you who stated " If you compile and execute this code snippet (...)"?
Well, I always use an IDE for that!

But in all sincerity: is that an exam question (or could such a question be asked)?
If so, what exactly are they testing here?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!


Next time I'll explicitly mention "using javac and java respectively"

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?


No, this one will definitely not be on a certification exam! It's waaay too crazy Just for fun!
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good insight into the power of Unicode!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After 4 weeks it's probably time to reveal the "magic" behind the code and the bizarre output at first sight.

As you probably could have imagined, the comment in the code snippet is responsible for what's happening hereIn 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 {
  • Note that this happens before the compiler actually reads and interprets your source code. After doing this, the program looks like thisSo now it should be really obvious why "true is false!" is printed, because the println statement is not part of the if block

    And just for fun. You can use Unicode escape sequences to entirely write a class (or even an application). This code snippet is exactly the same as the other code snippets in this post, but uses nothing but Unicode escape sequences Needless to say it should be avoided in actual coding as the readibility is pretty poor

    Hope it helps!
    Kind regards,
    Roel

    [edit] converted spaces to Unicode escape sequence (Thanks Campbell)
     
    Ranch Hand
    Posts: 182
    18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 {



    I guess , i have answered it right ..
     
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And I spent a good two minutes trying to remember what \u000a meant I could have saved myself the bother by reading the whole thread.

    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.
    And why have you got a space after \u0073 in line 17?
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:. . . Java Puzzlers by Joshua Bloch. Jochua calls this puzzle as Line Printer. . . . .

    That appears to be on page 35.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.


    True! But if you don't know what's going on, it definitely looks like a comment. So that's why I decided to use "comment".

    Campbell Ritchie wrote:And why have you got a space after \u0073 in line 17?


    Because that would really be a tad too much, it would result in completely unreadable code I simply forgot to convert the spaces to their Unicode escape sequence \u0020. Fixed!
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:. . . it definitely looks like a comment. . . .

    And if that isn't a good puzzler, I don't know what is!
     
    Bartender
    Posts: 1210
    25
    Android Python PHP C++ Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I didn't know javac does this. It strikes me as a good candidate for implementing the kind of backdoor that was found in Juniper's firmware, speculated to be done using C #define trickery.

     
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Its indeed a security risk, only a trained eye will be able to spot something like this in an editor (especially if its a huge line of code...)



    Output:

     
    Karthik Shiraly
    Bartender
    Posts: 1210
    25
    Android Python PHP C++ Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:


    Sums up the foreign policies of some governments nowadays
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And let's have another one! What is the output of this code snippet?
     
    Ramya R Subramanian
    Ranch Hand
    Posts: 182
    18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have a partial answer. Not sure how this unicode "// File: code\com\united\Airplane.java" tweaks the output.

    But if doesn't then we have \u0020 - space and '\u0021' - exclamation

    line 7 must print first row - I
    line 8 - space
    line 9 second row - am
    line 10 - flying.space
    line 11 third row- whiiiiii!
     
    Tim Cooke
    Sheriff
    Posts: 5555
    326
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well I get this:
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ramya Subraamanian wrote:I have a partial answer. Not sure how this unicode "// File: code\com\united\Airplane.java" tweaks the output.


    It certainly does! The comment on the first line results in a compiler error (as illustrated in Tim's post). 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: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 that's also not 100% accurate! If you closely examine the code snippet, you'll notice that only the last statement is a println, all others are print statements. So all the output will be printed on the same line. But you are correct about what's printed. If you remove the comment on the 1st line and run the program, the output is "I am flying. Whiiiiii!" (what you could expect from an airplane ).
     
    Ramya R Subramanian
    Ranch Hand
    Posts: 182
    18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    its strange.. if you remove "com" from this line. it compiles fine.



    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.



    I was not aware of this.but i don't see them in the exam topics at the oracle site.
     
    Bartender
    Posts: 2236
    63
    IntelliJ IDE Firefox Browser Spring Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ramya Subraamanian wrote:its strange.. if you remove "com" from this line. it compiles fine.



    It is because \\ is escaped first, before a unicode escape.

    So, \\united translates to \\ + united (not \ + \united) which results in \united.

    An example:This prints:
    \\\u0075
    \\u
    \\u0075
    \u
    \u0075
    u
    u0075


    JLS mentions this situation:

    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.


     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ramya Subraamanian wrote:I was not aware of this.but i don't see them in the exam topics at the oracle site.


    That's because it isn't mentioned explicitly It's included in the section "Working With Java Data Types". You should know the char primitive data type and its range (from '\u0000' to '\uffff' (inclusive). And you should know the syntax of such a Unicode escape sequence: \u followed by four hexadecimal digits. And you should also know that a hexadecimal digit can be '0' through '9' and 'a' through 'f' (case-insensitive). So you should know that \unite is an invalid Unicode escape sequence as it contains illegal hexadecimal digits. Likewise you should also know that this code won't compile as wellAnd the reason is the same: usage of illegal hexadecimal digits.

    What you definitely not need to know for the exam is which character is represented by a Unicode escape sequence. So you definitely won't get a question like: "Which character corresponds with the Unicode escape sequence '\u0021'?".

    Hope it helps!
    Kind regards,
    Roel
     
    reply
      Bookmark Topic Watch Topic
    • New Topic