The interviewer will not answer when asked about basic data types. It is enough to read this article (collection is recommended!!!)

This blog mainly records some knowledge of Java data types, which are all basic knowledge that can no longer be basic, so do you really have a thorough grasp of it? Friends who need it, let's study together with the editor!

After reading this article, I believe that you will not be at a loss if someone asks about the type of data in the interview again.


What is the role of data types?

There are a lot of data in the program, and each data has related types, and the space occupied by different data types is also different. The function of the data type is to guide the JVM how much memory to allocate for the data when running the program space.

Classification of data types in JAVA

There are two types of data, basic data types and reference data types. The
basic data types are divided into four categories:

Integer (byte, short, int, long)
floating point (float, double)
Boolean (boolean)
character (char)

Reference types are divided into three categories:

Array
class
interface

Type occupied space and value range

Brief review

Before understanding the occupied space, let's briefly review some computer knowledge.

The computer can only recognize the binary system under any circumstances, for example: only recognizes 101010101010,
the bottom layer of modern computers uses alternating current, and the computer can only recognize 1 or 0;

What is binary?

A representation form of data. The decimal system represents the principle of full decimal, and the binary system represents the principle of full binary.

For example: decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 …
for example: binary
decimal: 0 1 2 3 4 5 6 7 8 9
binary: 0 1 10 11 100 101 110 111 1000 1001

Binary is to follow 1 2 4 8 16

Computers only recognize binary, so how do computers represent text?

Among the eight basic data types, byte, short, int, long, float, double, and boolean are all numbers at the bottom, which is very easy to represent on a computer. There is a fixed conversion rule between decimal numbers and binary.
But among the eight basic data types, the char type represents text in the real world, and there is no conversion relationship between text and binary.

In order for the computer to represent the text in the real world, we need further human intervention, and we need to manually formulate the comparison relationship between text and binary in advance. This conversion of the comparison relationship is called: character encoding.

The computer only supports English at first, and the first character encoding is: ASCII code
a is 97 when expressed by numbers, and A is 65 when expressed by numbers.

ASCII code can be understood as a dictionary, that is, each letter corresponds to a number to represent it, and finally the number is converted into binary, and then machine recognition is completed.

'a' --(decode according to ASCII) --> 01100001
01100001 --(code according to ASCII) -->'a'
When encoding and decoding are not the same set of comparison tables, garbled characters will occur.

Calculation unit conversion

1 Byte = 8 bit [1 byte = 8 bits] 1 bit represents a binary bit: 1/0
1 KB = 1024 Byte
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB

1 TB = 1024 * 1024 * 1024 * 1024 * 8 bit
is what we often hear about 1TB, it can store
Insert picture description here
so many 1s or 0s.

Simple understanding of bytes

Byte occupies 1 byte, so byte type data occupies 8 bits, and 8 bits are equivalent to 8 0 or 1.
Regarding the type of java numbers, numbers are divided into positive and negative, so there is a binary bit in the binary of the number called the sign bit. And this sign bit is on the leftmost side of all binary bits, 0 means positive number, 1 means negative number.

Known from the above: the maximum value of byte type binary representation: 01111111
2 to the 7th power -1 and the result is 127

So his value range is determined by the number of bytes.
The minimum value is -128 (the binary representation involves the original code, the inverse code, and the complement code, so you can understand it, but this article will not go into more detail)

Insert picture description here

Detailed use of eight types

int type

Declare an int variable, the code is as follows:
What needs to be noted here is that if the variable is declared but no value is assigned, the output will report an error! ! !

The int variable occupies 4 bytes in the memory, which is a 32-bit bit, which is 32 0 or 1, so the int type 5 is displayed on the computer like this:
00000000 00000000 00000000 00000000 00000101

byte type

The declaration method of byte type is the same as that of int type. The only difference is the number of bytes. To
declare a variable of type byte, the code is as follows:

short type

The short type is declared in the same way as the int type. The only difference is the number of bytes

When you see this, you will find that in fact, byte int short can be used to express numbers, the usage is the same, and int is used the most in development,
byte short is particularly rare, there is only one reason, int expression range is large.

Declare short type variable, the code is as follows:

long type

Since the value range of the long type is larger than that of the int and belongs to an advanced data type, it is necessary to distinguish it from the int type when assigning a value. It is necessary to add L or l (lowercase L) after the integer.

Declare long type variable, the code is as follows:

There is a common error here, that is, the long type is used, but the assignment does not include L. The int type is the default certificate type of JAVA. Without L, the default is int. Here you can see that the calculated b output is wrong. Because the number of bytes of the int type is so large. So it's not complete!

Floating point type (float, double)

Float and double are called floating-point types. The floating-point types represent numbers with a fractional part. The floating-point types in Java are divided into single-precision floating-point types (float) and double-precision floating-point types (double). They have different values. Value range.

By default, decimals are treated as doubles. If you want to use float to declare decimals, you need to add F or f after the decimal. In addition, you can use the suffix d or D to clearly indicate that this is a double type data, but there is no hard and fast rule on whether to add d or D. If you do not add F or f when declaring a float variable, the system will consider it as a double type and make an error.

Declare floating-point type variables, the code is as follows:

Note: Floating point values ​​are approximate values, and the result after calculation in the system may deviate from the actual value.

char type

The character class is used to store a single character, occupying 16 bits of memory space. When declaring a character variable, it should be expressed in single quotation marks, such as's' for a character.

Java can treat characters as integers. Because Unicode encoding uses unsigned encoding, it can store 65,535 characters, and char can do operations with integers.
Code example:

boolean type

Boolean type is also called logical type. There are only two values ​​of true and false, which represent true and false in Boolean logic. Boolean types cannot be converted to integer types.
Code example:

Data type conversion

Type conversion is the process of changing a value from one data type to another. There are two ways of data type conversion, namely implicit conversion and display conversion. If the low-precision data type is converted to the high-precision type, it will definitely succeed. The high-precision conversion to low-precision will inevitably result in information loss, or even failure.

The accuracy referred to here can be understood as the accuracy of the data.

Implicit conversion

From low-level type to high-level conversion, the system will automatically perform, without any operation, this type of conversion is called implicit conversion, can also be called automatic conversion.
These types are sorted from low to high precision: byte->short->int->long->float->double. The
char type is special, and it can be compatible with some int numbers without precision changes.

Code example:

Like high-precision to low-precision types, direct compilation and error reporting, you often encounter high-to-low conversion when developing programs. In this case, you need to use display conversion.

Explicit conversion

Syntax: (type name) the value to be converted.
We mentioned above that high to low is sometimes lost. Here I will write an example for you. I believe that the idea should be clear through this example.

Code example:
The reason for this situation can be understood as that you take a kettle and want to fill the water cup with water. The water cup can be filled with water is limited, so it can only be filled until it is full. At this time, there must be a part of the water that cannot be filled. Down. This will result in a loss!

When assigning an integer to a byte, short, int, or long type variable, the value range of the variable cannot be exceeded, otherwise the type conversion must be forced.
For example, if this scenario is directly compiled, an error will be reported. Although the output data is not 129 after the forced conversion, it directly avoids the compilation error.





Like it!

Hope more people can see it!

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/112208702
Recommended