search

Home  >  Q&A  >  body text

java - Byte自增问题

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吗
         */

求解,谢谢

PHPzPHPz2805 days ago505

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:58:08

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:58:08

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

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:58:08

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

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:58:08

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:58:08

    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.

    reply
    0
  • Cancelreply