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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
