Home  >  Article  >  Java  >  What is string class in java

What is string class in java

(*-*)浩
(*-*)浩Original
2019-05-22 09:21:133994browse

The String class in java is used very frequently, so let’s take a look at what it is?

What is string class in java

String is an immutable object

The java.lang.String class uses final modification and cannot be inherited. All literal values ​​in Java programs, that is, strings enclosed in double quotes, such as "abc", are implemented as instances of the String class.

String is a constant, and its object cannot be changed once it is constructed. In other words, the String object is immutable, and every method that appears to modify the String value actually creates a new String object to contain the modified string content. The original String object remains unchanged. The String object has read-only characteristics, and any reference pointing to it cannot change its value. Therefore, it will not have any impact on other references. But string references can be reassigned.

Java strings use Unicode encoding in memory. Any character corresponds to a two-byte fixed-length encoding, that is, any character (whether Chinese or English) is counted as one character in length and occupies two words. Festival.

Example:

public class Immutable {
      public static String upcase(String s) {
           return s.toUpperCase();
      }
      public static void main(String[ ] args) {
           String str1= "Hello World";
           System.out.println(str1);  //Hello World
           String str2 = upcase(str1);
           System.out.println(str2);  //HELLO WORLD
           System.out.println(str1);  //Hello World
     }
}

When str1 is passed to the upcase() method, what is actually passed is a copy of the reference. In fact, whenever a String object is used as a parameter of a method, a reference is copied, and the object pointed to by the reference actually stays in a single physical location and has never been moved.

String constant pool

The constant pool (constant pool) refers to some data that is determined during compilation and saved in the compiled .class file . It includes constants in classes, methods, interfaces, etc., as well as string constants. In order to improve performance, Java static strings (results of literals/constants/constant connections) are created in the constant pool, and try to use the same object and reuse static strings. For repeated string literals, the JVM will first search in the constant pool and return the object if it exists in the constant pool.

Example 2:

public class test1 {
    public static void main(String[] args){
        String str1 = "Hello";
        //不会创建新的String对象,而是使用常量池中已有的"Hello",
        String str2 = "Hello";
        System.out.println(str1 == str2); //true
        //使用new关键字会创建新的String对象
        String str3 = new String("Hello");
        System.out.println(str1 == str3); //false 
    }
}

The above is the detailed content of What is string class in java. 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