Home >Java >javaTutorial >What data type is string?

What data type is string?

(*-*)浩
(*-*)浩Original
2019-06-03 15:49:1243450browse

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;

What data type is string?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn