Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How does this code works ?

 
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 ?
 
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
Mac Safari Java
  • 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.
 
Sheriff
Posts: 22054
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic