+ 1
Postfix and prefix operators in Java
Can anyone explain why the output of this code is 33. Why a++ postfix operation inside the conditional is evaluated while the ++b prefix operation is not? Please explain to me this seems I am just learning and I want to get the logic about it. int a=2; int b=3; if(a++>b && ++b>0) System.out.println(1); else System.out.println(a+""+b); P.D: Explain this question using this exercise and try not to post the same lesson that is about the prefix and postfix operators here in Sololearn.
3 ответов
+ 14
Its not about prefix or postfix, but its about short circuiting in java
(expression1 && expression2) : here if expression1 is false, then result will be always false (value of expression2 doesn't matter, so if expression1 evaluates to false then it never checks for expression2).
so, a++>b (2++>3 which is false is only evaluated), after that a=3 & b=3(no change in b as ++b>0 not get evaluated) & else part get executed
print(a+""+b); //33
+ 10
Hy Rosana Rodríguez Milanés
I don't find lesson on short-circuiting in java, but you will find quizzes on it & might in comment section after lesson, so you can follow comment section after lesson for understanding lesson better & for more information
+ 2
Excellent answer, any recommendation or suggestion about the subject?..
There should be a lesson in the Java course about short circuiting in java.