Is String a basic data type?

1. Answer

String is not a basic data type, but a reference data type .

2. Analysis

2.1 What data types are there in Java?

The Java language is a strongly typed language (all variables must be defined before use). A clear data type is given for each data type. Different data types are also allocated different memory spaces, so the data sizes they represent are also different. the same.

1) Classification of data types

 2) Memory usage and value range of basic data types

type of data Keywords Memory usage (unit: byte) Ranges default value
integer byte 1 -128~127 0
short 2 -32768~32767 0
int 3 -2 to the power of 31 to 2 to the power of 31 -1 0
long 8 -2 to the power of 63 to 2 to the power of 63 -1 0L
floating point number float 4

Negative numbers: -3.402823E+38 to -1.401298E-45

Positive numbers: 1.401298E-45 to 3.402823E+38

0.0f
double 8

Negative numbers: -1.797693E+308 to -4.9000000E-324

Integer: 4.9000000E-324 to 1.797693E+308

0.0d
character char 2

0~65535

'u00000'
Boolean boolean 1 true、false false

Note: E+38 means multiplying 10 to the 38th power; similarly, E+45 means multiplying 10 to the 45th power.

3) Reference data type

Except for the 8 basic data types         in Java , all other data types are reference data types. The reference data type in Java language is actually equivalent to the pointer type in C language. Reference is actually a pointer, which is Points to the memory address of an object. What is stored in the reference data type variable is this memory address. Reference data types are used to represent complex data types. Common reference data types include: classes, interfaces, arrays, enumerations, etc.

For example: String class is a reference data type

2.2 The difference between basic data types and reference data types

1) Storage location

Basic data types : The specific contents of non-global basic data type variables defined in methods are stored in stack memory ;

Reference data type : As long as it is a reference data type variable, its specific content is stored in the heap memory , and what is stored in the stack is actually the memory address where the specific content is located; we can find the specific content of the variable through the variable address, for example You can find the room by the house number.

public class Test{
    
    public static void main(String[] args){
        //定义基本数据类型变量
            int i=1;
            double d=8.8;
        //定义引用数据类型变量
            String str="Heollo World";
                
    }
}

 2) Delivery method

Basic variable type : a non-global basic data type variable defined in a method, which is passed as a parameter when calling the method;

//基本数据类型作为方法参数被调用
public class Main{
   public static void main(String[] args){
       int msg = 100;
       System.out.println("调用方法前msg的值:\n"+ msg);    //100
       fun(msg);
       System.out.println("调用方法后msg的值:\n"+ msg);    //100
   }
   public static void fun(int temp){
       temp = 0;
   }
}

 Reference variable type : Reference data type variable. When calling a method, it is passed by reference as a parameter and a copy of the reference is passed.

//引用数据类型作为方法参数被调用

class Book{
    String name;
    double price;

    public Book(String name,double price){
        this.name = name;
        this.price = price;
    }
    public void getInfo(){
        System.out.println("图书名称:"+ name + ",价格:" + price);
    }

    public void setPrice(double price){
        this.price = price;
    }
}

public class Main{
   public static void main(String[] args){
       Book book = new Book("Java开发指南",66.6);
       book.getInfo();  //图书名称:Java开发指南,价格:66.6
       fun(book);
       book.getInfo();  //图书名称:Java开发指南,价格:99.9
   }

   public static void fun(Book temp){
       temp.setPrice(99.9);
   }
}

Guess you like

Origin blog.csdn.net/qq_61902168/article/details/128338038