post - how is that x=20;x= ++x + ++x + x++ ;final value of x in java is 65 -
this question has answer here:
how possible post increment operator should increase x 66?
when did same y= ++x + ++x + x++; gave value 65 y , 23 x.
so let me know how java compilers solving these expression.
let java show you. javap -c myclass
shows bytecode:
public static void main(java.lang.string[]); code: 0: bipush 20 2: istore_1 3: getstatic #2 // field java/lang/system.out:ljava/io/printstream; 6: iinc 1, 1 9: iload_1 10: iinc 1, 1 13: iload_1 14: iadd 15: iload_1 16: iinc 1, 1 19: iadd 20: dup 21: istore_1 22: invokevirtual #3 // method java/io/printstream.println:(i)v 25: return
and result logical if think it: have 2 preincrements , 1 postincrement. so, code in effect:
y = 0 x++ // 21 y += x x++ // 22 y += x y += x // (still 22!) x++ // 23 x = y // (21 + 22 + 22 @ point)
Comments
Post a Comment