请解释一下这段代码,我得到的输出为 6,任何人都请帮助我。
class A {
static int i=1111;
static {
i=i-- - --i;
}
{
i=i++ + ++i;
}
}
class B extends A {
static {
i=--i - i--;
}
{
i=++i + i++;
}
}
public class Shadow2 {
public static void main(String[] args) {
B b = new B();
System.out.println("Find->" + b.i);
}
}
输出
Find->6
谁能帮我看一下代码
first in static A->
i=i-- - --i; will be 2 becoz, i-- means first assign value and then decrement the value so,i--=1111 then in satic memory i will be 1110 then --i means first decrement and then assign so, in this 1110 will become 1109 and then value of i wiil be 2.
secondly in static B->
currently i is 2. i=--i - i--; will be i=1-1 i will be zero
third instance block of A will be executed->
i=i++ + ++i will be i=0+2=2
finaly instance block of B will be executed->
i=++i + i++ will be i=3+3=6
答案是6。
第一个静电
i=i-- - --i; // (1111) - (1110-1) // result 2
i=--i - i--; // (1 - 1) // result 0
i=i++ + ++i; // (0 + [1+1]) // result 2
i=++i + i++ // ( [2+1] + [3]) //6 // final output 6