r/javahelp • u/[deleted] • Mar 16 '19
Solved I'm a beginner learning Java in school.We use BlueJ. I need help understanding incrementing and decrementing.
For this question:
int i=0;
boolean t=true;boolean f=false,b;b=(t && ((i++)==0));
b=(f && (i+=2)>0);
System.out.println(i);
By employing what my teacher taught me, I concluded that the value of i is 3, but the correct value, as I found out, is 1.
Could someone please explain to me how I should evaluate such problems/questions ?
16
u/shagieIsMe Extreme Brewer Mar 16 '19
This... is ugly code. Lets do some transformations on it first to make it something more readable.
First, formatting. Four spaces indent makes it look better.
public class Main {
public static void main(String[] args) {
int i=0;
boolean t=true;
boolean f=false,b;
b=(t && ((i++)==0));
b=(f && (i+=2)>0);
System.out.println(i);
}
}Next, that t and f thing is unnecessary.
It's going to complain about unused variables and always false (when you're using a good IDE)... but I'm going to ignore that.
There are several things going on here. The most important is boolean short circuiting.
public class Main {
public static void main(String[] args) {
int i=0;
boolean b;
b=(true && ((i++)==0));
b=(false && (i+=2)>0);
System.out.println(i);
}
}If you have boolean qux = doFoo() && doBar() and doFoo is false, it is impossible for qux to be true. So... it doesn't even bother calling doBar (example on ideone).
In this case, i++ will be evaluated. There's no other hijinks with the post increment (some people like having students evaluate i++ + ++i which, while well defined in Java is still a pain).
The i+=2 part will not be evaluated. false && doAnything will always be false so doAnything won't evaluate.
There's a similar situation when || is used for short circuiting. There, if at any time the expression evaluates to true, there's no point in calling anything else.
I am going to strongly encourage you to recognize this code and never ever write code that mutates state within the expression of a conditional. It's a pain to track down what is actually happening (and then someone comments out the block of code for an if statement and the code starts behaving differently... just don't do it).
The specification for this is JLS 15.23 Conditional-And Operator &&
The conditional-and operator
&&is like&(§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
The conditional-and operator is syntactically left-associative (it groups left-to-right).
The conditional-and operator is fully associative with respect to both side effects and result value. That is, for any expressions a, b, and c, evaluation of the expression
((a) && (b)) && (c)produces the same result, with the same side effects occurring in the same order, as evaluation of the expression(a) && ((b) && (c)).
Each operand of the conditional-and operator must be of type boolean or Boolean, or a compile-time error occurs.
The type of a conditional-and expression is always boolean.
At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (§5.1.8).
If the resulting value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated.
If the value of the left-hand operand is true, then the right-hand expression is evaluated; if the result has type Boolean, it is subjected to unboxing conversion (§5.1.8). The resulting value becomes the value of the conditional-and expression.
Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
7
4
u/desrtfx Out of Coffee error - System halted Mar 16 '19
You need to approach this line by line.
The first couple lines are just declarations and initializations. They should be fairly clear.
b=(t && ((i++)==0));- here we have the first tricky one.One thing important to know is that
&&(AND) is short circuiting - this means that the condition is only evaluated to the point where the result can no longer change.The first part of the condition is
t- this evaluates totrue. Sincetruedoes not finalize the result of the condition, the part after the&&gets evaluated.((i++) == 0)- here, you have to go inside out:(i++)- this is a postfix increment - this means that the original value ofiis used for the rest of the equation, so the equation evaluates toi == 0which yieldstruebecause at that pointihasn't been incremented.
Now, the whole terms evaluate to
true && truewhich yieldstrueand that will be the value ofbat the same time
iwill be incremented (++) and thereforei = 1
b=(f && (i+=2)>0);- second part. Again, you have to start step by step:f- this isfalseand sincefalse && <whatever>will always result infalsethe rest of the term will not be evaluated. This also means that the part((i+=2)>0)will never be executed.
Finally, since
iwas1from the first part and not changed in the second part, the final result will be1
2
u/helicoid Mar 16 '19
You have some pretty solid explanations already but maybe this will help a little.
If I have if(a && b) then both a and b need to be true. If a is false java won't bother going to b.
When you come across this line: b=(t && ((i++)==0));
t is true so java goes to the right of the && and the i++ is encountered making i equal to 1.
When you come across this line: b=(f && (i+=2)>0); f is false so java won't bother going to the right of && and the i+=2 is never executed.
1
u/matt08220ify Mar 16 '19 edited Mar 16 '19
Holly jumping monkey balls why even use a boolean? I'm by no means an expert, it took me no joke 20 minutes to decipher your code.
edit: Sorry that came off the wrong way. If the first operand is false java will not bother checking the second, and if the first is true it will continue to the second operand(which in this case contains an operation either i++ or i +=2) and assess whether or not it is true; this make's the end result of the boolean pointless in regards to the value of i as java will perform the operation on i as long as the first operand was true (in this case). You could get the value of i to be 3 using the same exact amount of code you have here organized in a different way; you would have to put the operation of i before the boolean value; again this will word because java needs to perform the operation to assess whether or not the operation yields a result equal to, less than, or greater than 0. Also it doesn't even matter whether or not you are assessing the operation to be equal to, less than, or greater than zero because as I said java has to perform the operation on i to know, which obviously sets the value of i
-3
u/trisul-108 Mar 16 '19
i+=2; b=(f && (i>0));
3
u/desrtfx Out of Coffee error - System halted Mar 16 '19
Not only is this completely wrong, it is also not helpful since there is no explanation.
The
i+=2part in OP's original code will never be executed due to the short-circuiting nature of&&and||.0
u/trisul-108 Mar 16 '19
I thought he wanted it executed, as he was expecting to get i==3.
1
Mar 16 '19
The way my teacher taught me,with an example not too different from the one in my question,made me understand things differently. Also,she made no mention of short-circuiting. The whole class was confused with this because of the teacher lol. I didn't want it as i==3 but wanted to know how i==1,and now that's been clarified with some well-detailed(and explained) answers,so I understand this better.
10
u/TooLateQ_Q Mar 16 '19
This question is about understanding how short circuit boolean operators work ik Java. I+=2 is never executed because when you do false && other stuff the other stuff is never executed as we already know the result will be false