Storage principle of reference types: Reference types inherit from the Object class (also reference types) and store data according to the memory model of storing objects in Java. This type of data is stored using the Java memory heap and memory stack. Storage, simply put, "references" are stored on the ordered memory stack, while the value of the object itself is stored on the memory heap;
Difference: The difference between basic data types and reference types is mainly that basic data types are allocated on the stack, while reference types are allocated on the heap (requiring the concepts of stack and heap in Java),
What data type does string string belong to in Java?
String in Java is a reference data type. Because String is a class.
Note: String class is immutable, so once you create a String object, its value cannot be changed (see the notes section for details).
The String class has 11 construction methods. These methods provide different parameters to initialize the string, such as providing a character array parameter:
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n', 'o', 'o', 'b'}; String helloString = new String(helloArray); System.out.println( helloString ); } }
The compilation and running results of the above example are as follows:
runoob
The difference between the two ways of creating a string object
The direct assignment method creates the object in the constant pool in the method area
String str="hello";//直接赋值的方式
The string object created through the construction method is in the heap memory
String str=new String("hello");//实例化的方式
The above is the detailed content of What data type is string?. For more information, please follow other related articles on the PHP Chinese website!