Java Serial 72-String Class Details, Multiple Construction Methods

Posted by loquela on Mon, 13 Jan 2020 17:55:03 +0100

1. String Class

1.String classes are not mutable, that is, after a String object is declared

2.java.lang.String; is a string type

(1) Once a string is created, it can no longer be changed, "abc" string object cannot be changed to "abcd" once it is created.

(2) Enhance string access efficiency: Cache technology is used in the program, so all strings enclosed in double quotation marks in java will be created in the String Constant Pool, which is stored in the method area.

(3) During program execution, if a program uses a string, such as "abc", the program searches for the string in the string constant pool. If it cannot find the string, it creates a new "abc" string in the string constant pool, and if it finds it, it takes it directly for use.(The string constant pool is a cache to improve the efficiency of accessing strings).

(4) When using Sting, you should pay attention to the following issues: try not to do string splicing frequently, because once a string is created, it can not be changed. As long as the string splicing frequently, a large number of string objects will be created in the string constant pool, which will cause problems for garbage collection.

 

package com.bjpowernode.java_learning;

​

public class D72_1_StringClassAnlysis {

  public static void main(String[] args) {

    //Create a " abc"String object, the memory address of the object, allows s1 Variable save.

    //s1 Is a reference, s1 Point to " abc"object

    String s ="abc";

    //Can let s1 Point again? s1 Is a local variable, s1 No before final,therefore s1 Can be redirected.

    //however"def"The string itself is immutable.

    String s1 = "Androgen";

    String s2 = "Androgen";

    System.out.println(s1==s2);

    System.out.println(s1.equals(s2));

    //Compare the values of two strings, do not use double sign, use function.equals,Double equal sign compares memory addresses

    //If the string is new Out of the box, the memory addresses are definitely not equal, as demonstrated by this example.

    //A pool of string constants stored in a method area if strings are used directly

    String s3 = new String("Androgen");

    String s4 = new String("Androgen");

    System.out.println(s3==s4);

    System.out.println(s3.equals(s4));

   

    System.out.println(s1==s3);

    System.out.println(s1.equals(s3));

   

   

    //Problem: The following program creates three objects, two in heap memory and one in method area.

    String s5 = new String("abc");

    String s6 = new String("abc");

   

    String[] ins = {"sport","music","food","sleep"};

    //Requires the above interests to be stitched together into a string

    String temp = null;

    for(int i=0;i<ins.length;i++) {

      if(i==ins.length-1) {

        temp += ins[i];

      }else {

        temp += ins[i]+",";

      }

    }

    System.out.println(temp);

    //This is not recommended because there is a lot of stitching and the method area creates a lot of strings

   

  }

}

2. Common construction methods of strings

 

package com.bjpowernode.java_learning;

​

public class D72_2_StringCommonMethod {

  public static void main(String[] args) {

    //Common construction methods

    String s1 = "abc";

    String s2 = new String("abc");

    byte[] bytes = {97,98,99,100};

    String s3 = new String(bytes);

    System.out.println(s3);//As you can see from the output, String Has been rewritten Object In toString

   

    String s4 = new String(bytes,0,2);//The second parameter is the start position, and the third parameter is the length

    System.out.println(s4);

   

    char[] c1 = {'I','yes','in','country','people'};

    String s5 = new String(c1);

    System.out.println(s5);

   

    String s6 = new String (c1,2,2);

    System.out.println(s6);

   

  }

}

​

3. Source Code:

D72_1_StringClassAnlysis.java

D72_2_StringCommonMethod.java

https://github.com/ruigege66/Java/blob/master/D72_1_StringClassAnlysis.java

https://github.com/ruigege66/Java/blob/master/D72_2_StringCommonMethod.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Topics: Java github Big Data