What's output of this code ? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

What's output of this code ?

class precedence { public static void main(String[] a) { System.out.println(5-3+12/3*2); } }

24th Feb 2017, 10:04 AM
Preet Patel
Preet Patel - avatar
8 Antworten
+ 2
precedence order is %, /,*,+,-
24th Feb 2017, 5:23 PM
Arshit patel
Arshit patel - avatar
+ 1
Multiplication and division have the same precedence so they will evaluate from left to right.
24th Feb 2017, 10:22 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
And what about precedence order of %,/,* ?
24th Feb 2017, 10:30 AM
Preet Patel
Preet Patel - avatar
+ 1
* / % all have the same precedence + - have the same precedence https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
24th Feb 2017, 10:36 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks buddy @Ap_star_._
24th Feb 2017, 5:49 PM
Preet Patel
Preet Patel - avatar
0
5-3+12/3*2 = 10 12/3 = 4 4*2 = 8 5-3 = 2 2+8 = 10
24th Feb 2017, 10:13 AM
ChaoticDawg
ChaoticDawg - avatar
0
For 2*3/6 O/P is 1, why so ? Because in previous case division is given more precedence while multiplication in the latter.
24th Feb 2017, 10:18 AM
Preet Patel
Preet Patel - avatar
0
Thank you @ChaoticDawg
24th Feb 2017, 10:39 AM
Preet Patel
Preet Patel - avatar