Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of Java basic data types

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

What are the knowledge points of this article "what are the knowledge points of Java basic data types?" most people do not understand them, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the knowledge points of Java basic data types?"

I believe those of you who have studied Java will know that Java is a strongly typed language, so Java's specification for data types will be relatively strict.

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

Basics

Let's start with some practical information!

Basic data type size range default wrapper type boolean-true,falsefalseBooleanchar16 bit [0216-1]'\ u0000'Characterbyte8 bit [- 128127] 0Byteshort16 bit [- 215215-1] 0Shortint32 bit [- 231231-1] 0Integerlong64 bit [- 263263-1] 0LLongfloat32 bit [- 3.4E38pr 3.4E38] 0.0FFloatdouble64 bit [- 1.8E308Magol 1.8E308] 0.0DDouble

If you have mastered these key points, it shows that the foundation is good. If you are interested, you can learn more about it with the editor!

In-depth

In the computer language, the data type is the abstraction of the data processed by the program, and the meaning of its existence is to facilitate the virtual machine to allocate more appropriate space for different data. Therefore, the understanding of data types is naturally inseparable from virtual machines.

In the Java virtual machine specification, boolean types are mapped to int types. Specifically, "true" is mapped to the integer 1, while "false" is mapped to the integer 0. This coding rule restricts the implementation of Java bytecode.

The Java virtual machine specification also requires the Java compiler to follow this coding rule and use integer-related bytecodes to implement logical operations and conditional jumps based on boolean types. As a result, in the compiled class file, except for the fields and incoming parameters, there is almost no trace of the boolean type.

The basic types of Java have corresponding ranges and default values.

As you can see, the ranges of byte, short, int, long, float, and double are enlarged in turn, and the preceding ranges are included by the following ranges. Therefore, there is no need to cast from the previous basic type to the later basic type. It is also worth noting that although their default values look different, they are all 0 in memory.

Boolean and char are the only two unsigned types among these basic types. The range of values of the boolean type is 0 or the range of values of the 1-minute char type is [0, 65535]. Usually we can assume that the value of the char type is a non-negative number, which is very useful, for example, as an array index.

Data storage

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

In the Java virtual machine specification, the local variable region is equivalent to an array and can be indexed by positive integers. Except for long and double values that need to be stored in two array units, values of other basic types and reference types occupy one array unit. In other words, boolean, byte, char, and short take up the same space on the stack as int, and the same as reference types.

Therefore, when we store a value of type int in these types of fields or arrays, it is tantamount to an implicit masking operation. For example, when we store 0xFFFFFFFF (- 1) in a field declared to be of type char, because the field occupies only two bytes, the upper two bytes are truncated and eventually stored in "\ uFFFF".

Data loading

The arithmetic operation of Java virtual machine depends almost entirely on the Operand stack. That is, we need to load the boolean, byte, char, and short in the heap onto the Operand stack, and then calculate the values on the stack as int types.

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

For byte and short, loading is accompanied by symbolic extensions. For example, the size of short is two bytes. The value of short is also copied to the lower two-byte of type int when loaded. If the short value is non-negative, that is, the highest bit is 0, then the high two bytes of the value of the int type will be filled with 0, otherwise 1 will be filled.

The above is the content of this article on "what are the knowledge points of Java basic data types". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report