Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

# Understanding this code output

Greenhorn
Posts: 4
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.

Greenhorn
Posts: 3
& 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

Billy Smith
Greenhorn
Posts: 4
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.

Greenhorn
Posts: 29
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

Marshal
Posts: 61700
193
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: 61700
193

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
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: 61700
193

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
Thanks so much! I finally understand a problem I have been thinking about for the entire day!

Campbell Ritchie
Marshal
Posts: 61700
193
That's a pleasure

 It is sorta covered in the JavaRanch Style Guide.