The difference between byte + = and direct addition

Check out the code below:

public class ByteTest {
    public static void main(String[] args) {
        byte a = 1;
        byte b = 2;
        a = 1+b;
//        a+=b;
    }
}

 

Found wrong, the compiler prompts you to provide an int type of data, but what you need is a byte type.

The reason is: byte and short data, when calculating, it will automatically prompt that the data type is int , so the result type on the right side of the equal sign is int, need to be forced to byte type.

Let's take a look at the result of the 7th line of code:

The compiler is normal and the result is correct because the operators such as "+ =", "++", etc. do not have automatic type promotion

Let's take a look at the calculation result of the following code:

The calculation result is beyond our expectation, why the byte 127 plus 1 becomes -128?

We know that the range of values ​​of basic data types is as follows:

Types of Byte length   Ranges
int  4 bytes  -2 147 483 648 ~ 2 147 483 647
short 2 bytes   -32 768 ~ 32 767
long  8 bytes  -9 223 372 036 854 775 808 ~ 9 223 372 036 854 775 807
byte  1 byte   -128 ~ 127
float 4 bytes About ± 3.402 823 47E + 38F (6 ~ 7 significant digits)
double  8 bytes  About ± 1.797 693 134 862 315 70E + 308 (15 significant digits) 

The maximum value of byte is 127. The following is part of the source code of Byte wrapper:

public final class Byte extends Number implements Comparable<Byte> {

  
    public static final byte   MIN_VALUE = -128;

    public static final byte   MAX_VALUE = 127;

   
    @SuppressWarnings("unchecked")
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }

    private static class ByteCache {
        private ByteCache(){}

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }
......

This packaging class maintains a private static internal class inside and out, caches the packaging type corresponding to the direct byte of -128 to 127, and returns the result through the value of this static method. At this time, we get the same byte value corresponding to The packaging objects are all the same.

The following explains why the result is -128:

As you know, the byte type corresponds to 1 byte, that is, 8 bits. The leftmost bit is the sign bit. In binary, 127 is 01111111. If you want to add one to 127, the result is 0111 111 becomes 1000 0000. Negative numbers are stored in the computer and stored in the form of complements. The first bit on the left is the sign bit.

 The complement of negative numbers in java is converted to decimal as follows:

If a number is positive, its original code, inverted code, and complement are the same; a positive number's complement is converted to decimal and can be directly converted.

Know the complement of a negative number and convert it to a decimal number. Steps:

      1. Reverse everyone first;

      2. Convert it to a decimal number;

      3. Add a minus sign and then subtract 1.
Follow the above steps: the result of 1000 0000 is, the first step: 0111 1111, the second step: 127, the third step: -127-1 = -128, which is the final result.

 

Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/105363822