Java also provides a byte data type, which is a basic type. Java byte is processed as the smallest number, so its value range is defined as -128~127, which is signed byte. The following article mainly introduces you to the relevant information about the byte type in Java. Friends in need can refer to it.
【Recommended learning: java video tutorial】
Introduction
byte, that is, byte, consists of 8-bit binary. In Java, byte type data is an 8-bit signed binary number.
In computers, the value range of 8-bit signed binary numbers is [-128, 127], so in Java, the value range of byte type is also [-128, 127].
Value range analysis
I have been wondering why it is not -128 to 128? I analyzed this issue today.
First of all, we have to understand one thing, that is, the operation rules:
The highest bit of a positive number is 0, and the value of a positive number is the value represented by binary.
The highest bit of a negative number is 1. The value of a negative number is inverted, plus one, and then a negative sign is added to get the value.
We use 8-bit binary to illustrate this rule:
For example: 00000001. If the highest bit is 0, it is a positive number, so it represents 1 in decimal.
Another example: 10000001. The highest bit is 1, which is a negative number. What is the value? Negate it to get 01111110 and add 1 to get 01111111, then the value is -127
Understanding this operation rule, we officially start talking about byte, which is exactly an 8-bit binary number. short is 16 bits, int is 32 bits, long is 64 bits.
It is not difficult to understand that the maximum positive number of byte is 01111111 (the highest bit must be 0), which is 127.
Then you may think that the smallest negative number of byte is 11111111, right? If you think so, you are totally wrong. Let's see what the binary number 11111111 represents.
According to the above tips, we know that this is a negative number. Its value is negated first and then 1 is added.
11111111 is inverted to get: 00000000, add 1 to get 00000001. The final value obtained is -1.
This is the largest negative number. From this, have you ever thought that the smallest negative number would be 10,000,000?
Let's do the math and negate: 01111111, add 1 to get 10000000, and finally get -128.
127 is 01111111, but -128 is 10000000, and we see a strange thing.
Look carefully at these two binary numbers. Do you get the latter by adding 1 to the former? right.
You can write a small program to experiment:
byte a = 127; a+=1; System.out.println(a);
The result is exactly -128
From this we can see that the binary number ranges from 00000000 to 01111111 to 10000000 to 11111111
That is, the decimal number ranges from 0 to 127 to -128 to -1.
Next, we use a piece of code to understand byte more deeply: public class A {
public static void main(String[] args) {
int b = 456;
byte test = (byte) b;
System.out.println(test);
}
}
456 is 111001000. Since int is a 32-bit binary, in the computer, it is actually 00000000000...111001000. When int is converted into byte, the computer will only retain The last 8 digits are 11001000.
Then the highest bit of 11001000 is 1, which means it is a negative number, and negative numbers are stored in two's complement form in computers, so we calculate the original code of 11001000 as 00111000, which is 56, so 11001000 It represents -56, so the final test value is -56.
This article comes from the
java introductionThe above is the detailed content of Detailed introduction to the byte type in java. For more information, please follow other related articles on the PHP Chinese website!