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
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# How does this code works ?

Ranch Hand
Posts: 55
• Number of slices to send:
Optional 'thank-you' note:
public class NewClass {
public static void main(String [] args) {
boolean x = true;
boolean y = false;
short z = 42;

if((x = false) || (y = true)) z++; // Why gives 43 !?
if((z++ == 44) || (++z == 45)) z++; // Why gives 46 !?

System.out.println("z = " + z);
}
}

How does this code works ?

Ranch Hand
Posts: 1683
• Number of slices to send:
Optional 'thank-you' note:

if((x = false) || (y = true)) z++; // Why gives 43 !?

When you have a conditional OR operator where only one of the operands is true, then true is returned. So:

false || true returns true.

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by podonga poron:
...
if((x = false) || (y = true)) z++; // Why gives 43 !?
if((z++ == 44) || (++z == 45)) z++; // Why gives 46 !?
...

There are quite a few details here. Which parts are you questioning?

If you tell us how you expected this to work, then we can help you find the problem.

podonga poron
Ranch Hand
Posts: 55
• Number of slices to send:
Optional 'thank-you' note:
i taked the code from the online practice certification for SUN java programer ..

they gives 4 options, and "z=46" is the one, but i don't understand how it arrives to that result ...

BTW, what is the difference betwen ++z and z++ ?

thanks !

OT: what means i += 5 ?

[ May 23, 2008: Message edited by: podonga poron ]
[ May 23, 2008: Message edited by: podonga poron ]

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
++z is pre-increment: it adds 1 to the variable z, and the result of the expression is the new value of z.

z++ is post-increment: it adds 1 to the variable z, and the result of the expression is the old value of z.

i += 5 is a short way to write i = i + 5.

marc weber
Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
In this code, you might also note that || is a short-circuiting version of |, and that = is an assignment rather than the comparison operator ==.

But again, I think it would help if you posted your understanding of what this code does so that we know how to help you.

Sheriff
Posts: 22773
130
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by marc weber:
and that = is an assignment rather than the comparison operator ==.

Exactly. While you might think it is testing whether or not x is false, it is assigning false to x, then evaluating only x.

While in this case it won't matter that much (x == false is false since x is true; x = false will assign false and then evaluate to false), there are cases where it will:

While comparison will return false, the assignment will cause it to return true, and therefore z will be increased.

 What's wrong? Where are you going? Stop! Read this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com