我无法理解这段代码,我得到 6 作为输出?
I cant able to understand this code i am getting 6 as output?
请解释这段代码,我得到的输出是 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,任何人都可以帮助我。
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