Java Basics - basic types

Posted by Volte on Sun, 13 Feb 2022 16:10:28 +0100

New JAVA

java learning starts with basic types, which are often used in daily work. But are you sure you have mastered the basic types of knowledge?
Let's take a look at the next few questions

1,boolean What is the length of the type?
2,void Is it a basic type?
3,int i=0; Integer j=i;In this boxing process, how is the basic type assigned to the reference type?
4,Basic types are stored on the stack. When are they stored on the heap?

The answers to these questions are included in the following content. Let's study them together

1. What are basic types

   Java The types of objects in are divided into basic types and reference types, so how to distinguish between basic types and reference types.

The common way to simply distinguish between reference types and basic types is: basic types are stored on the stack; The reference is stored on the stack, and the data type is stored on the stack. But!!! The types are distinguished according to the data storage location. This method actually has some errors.

Look at the following code:

public void demo1(){    
    int tmp1=0;
}
tmp1 store in demo1 There is no doubt about this in the method stack of method, but what happens next?
public class Demo2 {
    int tmp2 =1;
}
	Demo2 Is a reference type, instantiating an object demo2 The data storage location is the heap, tmp2 yes int Type, if tmp2 The data is stored in the stack
,demo2 How to find the in the stack tmp2 What if there is another reference pointing to tmp2 A quote from? Obviously, it's impossible to design like this, so
 with tmp2 The actual storage location is in the heap.

Therefore, the storage locations of basic types are divided into the following cases:

1. When the basic type is used as a local variable in the method, it is stored on the method stack;

2. As an object attribute, if the object is stored on the heap, the basic type will be stored on the heap. (it is emphasized here that objects are stored on the heap. This involves compilation optimization, which will not be described in detail for the time being)

3. final and static modifications are stored in the method area

From my personal point of view, the key to distinguishing between basic types and reference types is their data storage structure,

1,Basic type data structure composition: the storage space allocated during creation, and the values contained in the basic type are stored;

2,The data structure of reference type consists of references on the stack and data on the heap, the storage space allocated on the heap, and the value of the object stored on the stack
 The storage space allocated on, and the storage reference points to the address in the heap.

Now that you know what basic types are, let's talk about basic types in detail

2. Classification, length and default values of basic types

Basic types are divided into four categories: integer, Boolean, generic and character

name	Length (bytes)	Default value
byte	1	0
short	2	0
int	4	0
long	8	0
float	4	0.0
double	8	0.0
char	2	\u000
boolean	Not specified	false

	One of the things worth studying is boolean,Because the official didn't give it clearly boolean The actual length is determined by the corresponding virtual length
 Machine to design. boolean It indicates yes and No. only one person is required. stay jvm The basic types are divided into: numerical type
boolean Type and returnAddress Three types. boolean As a separate type, I think it is a special type
 situation.
	But in jvm The smallest unit in is byte, that is to say boolean No less than 8. The more acceptable statement is, boolean
 Used during compilation int Indicates that it occupies 4 bytes. and boolean The array is represented by one byte.

Let's talk about the use of basic types

3. Unpacking and packing

   Unpacking is to convert the packaging type to the basic type

   Packing, that is, converting the basic type to the packing type

Unpacking and packing are often used in daily codes. The following code is a process of unpacking and packing

public void demo()
{
    //Packing
    Integer  no1 = 10;
    //Unpacking
    int no2 = no1;
}
This code is already familiar, but no1 In the process of assignment 10, how does a basic type become a reference type?
This is from java The syntax of the code begins
 Grammar sugar: refers to a kind of grammar added to the computer language. This grammar has no impact on the function of the language, but it is more convenient for programmers to use. ( Java There are many sugared grammars in, such as for Circulation String Splicing and so on. If you are interested, you can understand.)
in other words Integer  no1 = 10;This line of code has been sweetened. What has been done specifically. It's easy to decompile class Just look at the real face of this line of code in the file. The code before sugar is like this
Integer  no1 = Integer.valueOf(10);
int no2 = no1.intValue();

Now I understand the whole process of unpacking and packing!!!

Here is another ingenious design of packaging type. Let's take a look at the function valueOf

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

IntegerCache.low is - 128, integercache Hig is 127, that is, if the int value is between - 128-127, the returned result is integercache Cache the objects in the cache pool, that is, the returned results of the valueOf function, all point to an object within this range. If it is not within this range, a new Integer object will be returned.

So this is often asked during the interview

Integer  no1 = 10;
Integer  no2 = 10;
Integer  no3 = 200;
Integer  no4 = 200;
System.out.println(no1==no2);
System.out.println(no3==no4);

What is the output result of the above code?

Topics: Java Back-end