Detailed introduction to the byte type in java
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
