• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

And here's another Java Puzzler

 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output?

public class Main {
    public static void main(String...args) {

        http://www.coderanch.com/t/658698/ocajp/certification/true-equal-false

        for(int i = 0; i < 10; i++) {
            System.out.println("Hello " + +i);
        }
    }
}


(this post originated in this thread)
 
Campbell Ritchie
Sheriff
Posts: 51341
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello10, Hello9, Hello8 … Hello0 ??

I have seen that one before, so I know the answer.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:What is the output?

That's really a nice one as well, so it truly deserves its own thread. Have a cow!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Hello10, Hello9, Hello8 … Hello0 ??

Pretty sure Hello10 won't be part of the output...
 
Saed Hussein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and it will display:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9

be aware if you preincrement i and change your for loop body code like this result will be different and the code will display at round 0;
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:What is the output?

public class Main {
    public static void main(String...args) {

        http://www.coderanch.com/t/658698/ocajp/certification/true-equal-false

        for(int i = 0; i < 10; i++) {
            System.out.println("Hello " + +i);
        }
    }
}




LOL, good puzzler! TBH I didn't get it till I tried it out.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saed Hussein wrote:

and it will display:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9

be aware if you preincrement i and change your for loop body code like this result will be different and the code will display at round 0;


Commenting out of URLs is prohibited in puzzlers
 
Saed Hussein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be I don't have any idea for puzzlers.
 
Campbell Ritchie
Sheriff
Posts: 51341
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saed Hussein wrote:. . . result will be different and the code will display at round 0;
Are you sure you mean ++1? That will surely print Hello1 ten times. You probably meant ++i.

Now, I have seen that sort of puzzle before, but does anybody here know why it will compile and run apparently without errors?
 
Campbell Ritchie
Sheriff
Posts: 51341
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:. . . Pretty sure Hello10 won't be part of the output...
And surely the numbers would count up not down as I had it.
 
Liutauras Vilda
Bartender
Pie
Posts: 2762
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:What is the output?
Good one Pawel

And whoever getting ready for OCA exam, what will be the output now? Beware, some misdirection techniques has been used
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Saed Hussein wrote:. . . result will be different and the code will display at round 0;
Are you sure you mean ++1? That will surely print Hello1 ten times. You probably meant ++i.

That's incorrect! That statement will produce 2 compiler errors:
  • Syste should be System (but that's probably a typo)
  • And more importantly, 1 is a literal and you can't use the prefix increment operator on a literal


  • Campbell Ritchie wrote:Now, I have seen that sort of puzzle before, but does anybody here know why it will compile and run apparently without errors?

    I know but I don't want to spoil the fun for other ranchers finding about this Java puzzler
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saed Hussein wrote:may be I don't have any idea for puzzlers.

    That's the beauty of this Java puzzler, and I do know it looks (and sounds) very strange but it's really true: you don't need to comment the url out to successfully compile the code snippet So the code from the original post will compile without any errors
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22089
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Saed Hussein wrote:may be I don't have any idea for puzzlers.

    That's the beauty of this Java puzzler, and I do know it looks (and sounds) very strange but it's really true: you don't need to comment the url out to successfully compile the code snippet So the code from the original post will compile without any errors


    I think that we are so used to seeing URLs, that psychologically, it is incredibly difficult to see that line as anything but as an URL. It also didn't help that it was manually indented, so it didn't need to be in code tags. Good job Paweł !!

    Henry
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And whoever getting ready for OCA exam, what will be the output now? Beware, some misdirection techniques has been used


    the varargs syntax in the main method is wrong so the code doesnt compile
    it must be



    I guess if you modify it as above the Output will be the normal for a loop Hello 0 to Hello 9.



    all the + in this are all concatenation operators, it still works as if you add another +

    and its tricky because it looks like pre-increment operator at first , but no space inbetween + for a pre-increment operator


    this snippet will give output of 100 and 101 - in first statement it is concatenation and pre-increment in the second.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:the varargs syntax in the main method is wrong so the code doesnt compile

    Spot-on! The varargs syntax was indeed incorrect and that's why the code snippet doesn't compile.

    Ramya Subraamanian wrote:I guess if you modify it as above the Output will be the normal for a loop Hello 0 to Hello 9.

    Again correct!

    Ramya Subraamanian wrote:all the + in this are all concatenation operators, it still works as if you add another +

    That's incorrect! You only have one contatenation operator (the leftmost + sign) and all other + signs are the unary plus operator. So it's actually equivalent toIf it were all concatenation operators, the code would not compile because the string concatenation operator is a binary operator, meaning it requires two operands to do its job (and there are not). So if you would get rid of the i and change it with e.g. the empty String, this statement will not compile for the aforementioned reason

    Ramya Subraamanian wrote:this snippet will give output of 100 and 101 - in first statement it is concatenation and pre-increment in the second.

    Not 100% accurate! That's indeed the expected output, but the explanation is incorrect. Now you know (from the previous explanation) it is not the concatenation operator but the unary plus operator in the 1st statement.

    Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is it a Unary plus operator below as well ?



    why does this gives me a output of concatenation : your R is -->100100, your R is -->200200
     
    Campbell Ritchie
    Sheriff
    Posts: 51341
    86
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are four unary plus operators on lines 2 and 4 and two unary plusses on lines 2.
    You are getting 100100 for the same reason that you get such output with catenated Strings anywhere else. By the time you get to adding the number, your left operand is a String, so you use String catenation not addition.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:Is it a Unary plus operator below as well ?

    It's getting complicated here! It's a combination of unary plus operator and String concatenation operator. And as you know a String concatenation operator is a binary operator, so it requires two operands.



    Ramya Subraamanian wrote:why does this gives me a output of concatenation : your R is -->100100, your R is -->200200

    Let's focus on the first statementThe evaluation of the expression "your R is -->"+ R + + + + + R happens from left to right. The first operand is a String, so the first + sign is the String concatenation operator. The primitive int variable R is the right operand. So both values are concatenated resulting in a new String object "your R is -->100". This new String object will be the left operand for the next + sign, which is again the String concatenation operator (as the left operand is a String object as well). The right operand is the expression + + + + R (so the primitive int variable R with 4 unary plus operators). This expression evaluates to 100 which is concatenated to the String object "your R is -->100" resulting in the new String object "your R is -->100100" (which is printed). So this statement is equivalent to

    If you are interested in the addition of both R values, you need to add parantheses to both println statements as illustrated in this code snippetOutput:
    your R is -->200
    your R is -->400

    In the first line you now have a String concatenation operator (the first + sign), the addition operator (the second + sign) and 4 unary plus operator (third, fourth, fifth and sixth + signs). The first statement is equivalent to

    Hope it helps!
    Kind regards,
    Roel
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting puzzle, thanks Paweł.
    I understand such http is label here. The following line also compiles successfully:

    Additionally compiler will give error if we write the URL twice:

     
    Campbell Ritchie
    Sheriff
    Posts: 51341
    86
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes http: is a label. What follows after the double slash is of course a line-end comment. Which means I can go back to the first post in this thread where it was split from an earlier thread and warn you not to write
    \u000a
    inside the URL.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Which means I can go back to the first post in this thread where it was split from an earlier thread and warn you not to write
    \u000a
    inside the URL.

    Who says you can't write \u000a in the URL
     
    Campbell Ritchie
    Sheriff
    Posts: 51341
    86
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fortunately I said “warn” so this is only a warning not a definite error
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic