JavaSE basics---String class of common object API

In Java, the String class is used to encapsulate the string object. The advantage of this is that after the object is encapsulated, N multiple attributes and behaviors can be defined, and the common data such as string can be conveniently operated.

  Format: (1) String s1 = "abc"; //There is only one object "abc" in the memory pointed to by s1, create the abc string in the string constant pool, and assign the address to the s1 reference variable

       (2) String s2 = new String("abc"); //There are two objects abc and new in the content pointed to by s2. A String object is created by new in the heap memory. This object accepts the string "abc" during construction "Object.

      Note:  System.out .println (s1==s2);      //Output false, where == compares the address value

            System.out .println ( s1.equals (s2));   //Whether the contents of the compared strings are the same. Output true, the equals method defined in the String class overwrites the equals method in Object, and establishes the basis for the String class itself to judge whether the string objects are the same by comparing the contents of the strings. (The equals method in Object compares whether the address values ​​of two objects are the same)

  Features: Once the string is initialized, it cannot be changed and is stored in the constant pool in the method area

  Commonly used methods

    1. Construction method: convert a byte array or character array into a string

      String s1 = new String();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325249220&siteId=291194637