+ 5

Could you please explain the output of two Java statements? Topic: operators

int a = 2; int b = 3; 1) --b == --a // output true 2) a == b ++ && b == 3 // output true Could you please also tell me whether there is such a rule in Java, which demands executing the operators coming with variables before those coming with numbers? Something like here: a + b/2 == 2 // output true

11th Sep 2018, 4:29 PM
ZdzichPych
ZdzichPych - avatar
5 Respuestas
+ 6
i got false for both of these ? how are you getting true?
11th Sep 2018, 5:40 PM
D_Stark
D_Stark - avatar
+ 4
ZdzichPych /*i ran this in code playground and both returned false*/ int a = 2; int b = 3; boolean z = --b == --a; boolean y = a == b ++ && b == 3; System.out.print(z+" "+y);
11th Sep 2018, 9:22 PM
D_Stark
D_Stark - avatar
+ 3
Well, it looks this way: the first statement is presumably true on both Eclipse and SoloLearn's Playground, whereas the second one is true only on Eclipse. Meanwhile, I also feel the output should be false for both cases :/
11th Sep 2018, 7:30 PM
ZdzichPych
ZdzichPych - avatar
+ 2
Thank's for sharing your code, I write it without booleans, just: int a = 2; int b = 3; System.out.println(--b == --a); System.out.println(a == b++ && b == 3);
11th Sep 2018, 9:48 PM
ZdzichPych
ZdzichPych - avatar
+ 2
Hi! I tried to run what you did on code playground: public class Program { public static void main(String[] args) { int a = 2; int b = 3; System.out.println(--b == --a); System.out.println(a == b++ && b == 3); } } And the output was false false
4th Oct 2018, 7:16 PM
Kishalaya Saha
Kishalaya Saha - avatar