Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java basic data type, look at this article is enough!


May 30, 2021 Article blog


Table of contents


It is believed that the little partners who have studied Java know that Java is a strong-type language, so Java's specifications for data types are relatively strict.

It provides eight basic types (Void is not covered here). There are six numeric types (four integers (the default is int), two floating-point (default is double), one character type, and one Boolean.

foundation

Let's start with a little dry goods!!

The basic data type size range The default The type of wrapper
boolean - true,false false Boolean
char 16 bit [0,2 16 -1] ‘\u0000’ Character
byte 8 bit [-128,127] 0 Byte
short 16 bit [-2 15 ,2 15 -1] 0 Short
int 32 bit [-2 31 ,2 31 -1] 0 Integer
long 64 bit [-2 63 ,2 63 -1] 0L Long
float 32 bit [-3.4E38,3.4E38] 0.0F Float
double 64 bit [-1.8E308,1.8E308] 0.0D Double

If you master these points, that means the basis is good, interested words can be with the small editor to further understand! !

thorough

Data type in computer language, is the abstraction of the data processed by the program, it exists in order to facilitate virtual machines to allocate different data more appropriate space. Therefore, an understanding of data types is naturally inseparable from virtual machines.

In the Java virtual machine specification, the boolean type is mapped to the int type. S pecifically, "true" is mapped to integer 1, while "false" is mapped to integer 0. This encoding rule constrains the implementation of Java bytecode.

The Java virtual machine specification also requires the Java compiler to comply with this encoding rule and to implement logical operations with integer-related bytecodes, as well as condition jumps based on the boolean type. As a result, in the compiled class file, there is little trace of the boolean type except for fields and incoming parameters.

The basic types of Java have corresponding value domains and default values.

As you can see, the value fields of byte, short, int, long, float, and double expand in turn, and the previous value fields are contained by the value fields that follow. T herefore, there is no cast required to convert from the previous base type to the base type at the back. It is also worth noting that although their default values look different, they are 0 in memory.

Boolean and char are the only two unsigned types of these basic types. T he range of values for the boolean type is 0 or 1, and the range of values for the char type is 0, 65535. We can usually assume that the value of the char type is non-negative, which is useful, for example, as an array index.

Data storage

For every Java method called by a Java virtual machine, a stack frame is created. This stack frame has two main components, namely, the local variable area, and the bytecode operation stack.

In the Java virtual machine specification, the local variable area is equivalent to an array and can be indexed with a positive integer. I n addition to the long and double values, which need to be stored in two array cells, the values of the other basic and reference types occupy an array cell. That is, boolean, byte, char, short, the four types, the space occupied on the stack and int is the same, and the reference type is the same.

So when we store a value of an int type to a field or array of those types, it's the equivalent of an implicit mask operation. For example, when we store a 0xFFFFFFFF (-1) in a field declared as char type, because the field is only two bytes, the higher two bytes are intercepted and eventually deposited in the "-uFFFF".

The data is loaded

Almost all of the calculations of a Java virtual machine depend on the operan stack. That is, we need to load boolean, byte, char, and short into the operand stack in the heap, and then operate the values on the stack as int types.

For the two unsigned types, boolean and char, loading is accompanied by zero expansion. For example, the size of the char is two bytes, and when loaded, the value of the char is copied to the lower two bytes of the int type, while the high two bytes are filled with 0.

For both byte and short types, loading is accompanied by symbolic extensions. F or example, the size of short is two bytes. T he value of short at load time is also copied to a lower byte of the int type. If the short value is non-negative, i.e. the highest bit is 0, the higher byte of the value of the int type is filled with 0, otherwise it is filled with 1.

brief summary

To master the basic knowledge, the underlying implementation also needs to have a certain understanding. Understanding virtual machines can help you understand some of the nature of the Java language and help you use it.

Recommended reading:

Java: 23-day zero base fully started

Drill down into Java object orientation

Getting started with Java8 programming development