• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this code works ?

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
++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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic