java - Byte自增问题
PHPz
PHPz 2017-04-18 10:56:08
0
5
501
Byte a = 123;
        a++;
        System.out.println(a);// 124
        /*
         * 上面的结论是: a++的时候首先是 创建一个Byte对象124,然后将a引用指向这个Byte 124对象,这个解释有问题吗?
         */

        Byte b = 126;
        b = b++;
        System.out.println(b);// 126
        /*
         * 根据第一段代码的执行结果;
         * b=b++,的执行操作是首相将b引用赋值给b引用,所以没有改变,然后将b引用所指向的对象自增(这个实现的过程是创建一个对象值为127,
         * 然后让b引用指向这个127),如果这样理解,结果不应该是127吗
         */

求解,谢谢

PHPz
PHPz

学习是最好的投资!

reply all(5)
大家讲道理

b = b++: If you know C++, you can refer to the auto-increment implementation of ++, and you should be able to understand why it is 126

Byte operator++(Byte) {
    Byte temp = *this;
    this->value = this->value + 1;
    return temp;
}

can be understood as b is equal to temp before the operation is incremented

Ty80

++, --Be sure to write it separately, otherwise you will cause trouble for yourself.
For questions like i=i+++++i, I can only despise...

小葫芦

Resolved
http://blog.csdn.net/brooksyc...
Post to receive points

迷茫

++The difference between b and b++ is actually useless

巴扎黑

Java stack frame contains local variable table and operand stack. The ++ auto-increment operation is direct operation on the value in the local variable table. When i=i++, first push the i in the local variable table into the operand stack. Then add 1 to i in the local variable table to become 127, and then write i (126) in the operand stack back to i in the local variable table, covering the 127 data and changing it to 126. The order of ++i is different. It first increments i in the local variable table and then adds it to the operand stack.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template