• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding this code output  RSS feed

 
Billy Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int x = 4;
int y = 2;
System.out.println(x + 3 & y ++ + x * y);

I am new to Java and I do not understand why the output is 6. I have attempted to understand it, but I either got 7, or 2 as the output.
Can someone please help me understand what is going on?
 
Joseph Markovich
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& is bitwise AND operator comparing bits of each operand.
For example,

int a = 4;
int b = 7;
System.out.println(a & b); // prints 4
//meaning in an 32 bit system
// 00000000 00000000 00000000 00000100
// 00000000 00000000 00000000 00000111
// ===================================
// 00000000 00000000 00000000 00000100
 
Joseph Markovich
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should see 8 bits table, here => https://www.w3resource.com/php/operators/bitwise-operators.php
 
Billy Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the order of the operations and how do I get to the output of 6?
I know what each of the operators do, I'm just having trouble with getting 6, that's all.
 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This expression is complicated:

The trick is to put in parenthesis so it is clear which operators are done in what order. Now we have the expression:

The term x + 3 evaluates to 7 and y ++ + x * y evaluates to 14. We then compute 7 & 14 and come up with 6.

Does that make sense?

Bob
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of you, welcome to the Ranch

That link is to the implementation in PHP, and you can see that there are implementation details which are different from Java®.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Sherry wrote:. . . Does that make sense?

Bob
Afraid not; although your arithmetic appears correct, it is not at all clear why you get 14 from the right half, nor why using the & operator produces 6. Please explain more. Remember that some people don't remember whether y++ makes 2 or 3. I also think you need another pair of () around y ++.

Also, it is poor style to write y ++. The unary operator ++ should be joined to its operand without intervening spaces: y++.
Consider whether explaining the & operator would be easier if you denote all your numbers in hexadecimal.
 
Billy Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification. However, I do not get why the expression (y++ + x * y) evaluates to 14. It seems like the y++ = 2, and the y * x = 12, making y = 3.
I thought that the post increment only increments the value after the program statement is executed.
Does the y value have different values as the expression is being calculated? Why is that if we swap the unary increment with a decrement that we get the same value?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Billy Smith wrote:. . .
I thought that the post increment only increments the value after the program statement is executed.
Does the y value have different values as the expression is being calculated? Why is that if we swap the unary increment with a decrement that we get the same value?
I am afraid that is incorrect. You will find all the gory details in the Java® Language Specification (=JLS).
That JLS Section wrote:The value of the postfix increment expression is the value of the variable before the new value is stored.
The truth is that there are two values: the value of y which is now 3, and the value of the whole expression, which retains the old value of 2. In the case of y-- the whole expression will, similarly, have the old value of 2. You cannot see the new value of y until you next use it. Which is why using ++ and -- operators in conjunction with other arithmetic operators is such a bad idea, except in cert exam questions where they love confusing people with that sort of naughty code.
When you get to the next use of y, that has its new value of 3. I think you can now see how you get 14. Or maybe better 0xe.
 
Billy Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! I finally understand a problem I have been thinking about for the entire day!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!