File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How does this code works ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does this code works ?" Watch "How does this code works ?" New topic
Author

How does this code works ?

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
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

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
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

Joined: Aug 16, 2005
Posts: 14111
    
  16

++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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does this code works ?