Java data types-basic data types and reference data types

Data types in Java

  First of all, we must understand that the role of the data type is to determine how much memory space should be allocated to the variable during program operation .
  Data types in java include two major categories, one is basic data type , and the other is reference data type .

1. Basic data types

  The basic data types include four types and eight types, as shown in the following figure:
Insert picture description here  Details of the eight basic data types:

Insert picture description here

2. The difference between basic data types and reference data types

  • Basic data types. After such data variables are declared, Java will immediately allocate their memory space
  • Reference data type, similar to c\c++ pointer, it points to the object entity (specific value) in a special way .

note:

  • "==" is used to refer to the data type to determine whether the memory address is equal. To determine the content entity, you need to use equals.
  • The basic data type is a copy, and the original value remains unchanged after modification; the reference data type is an address, and the original value changes after modification
public class TypeOfData {
    
    
    public static void main(String[] args) {
    
    
        int num=100;
        int arr[]={
    
    1};
        System.out.println("基本数据类型num原值是"+num+"\t引用数据类型数组arr[0]原值是"+arr[0]);
        ChangeData(num,arr);
        System.out.println("基本数据类型num修改后的值" + num + "\t引用数据类型数组arr[0]修改后的值" + arr[0] );

    }
    public static void ChangeData(int num,int arr[]){
    
    
        //修改基本数据类型的值
        num=200;
        //修改引用数据类型的值
        arr[0]=2;

    }

}

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/109998744