Usage of String -- construction method

Keywords: Java

package cn.itcast_01;

/*

  • String: a string of data composed of multiple characters, or an array of characters
    *Through API, we know:
  •      A: string literal value, such as abc, can also be regarded as an object
  •      B: the string is a constant. Generally, it cannot be modified if it is assigned
  • Construction method:
  •  public String(): empty construction
  •  public String(byte[] bytes): convert byte array to string
  •  Public string (byte [] bytes, int offset, int length): converts a part of a byte array to a string
  •  public String(char[] value): convert character array to string
  •  public String(char[] value, int offset, int count): convert part of character array to character conversion
  •  public String(String original): convert string constant to string
  • String method:
  •  public int length(): returns the length of this string
  • */

public class StringDemo {

public static void main(String[] args) {
    //public String(): empty construction
    String s1 = new String();
    System.out.println("S1:" + s1);//Review the previous lesson: output an object directly, and the output result is the address value of the object; however, there is rewriting, so the address value will not be output
    System.out.println("s1.length():" + s1.length());
    System.out.println("-----------------------------------------------------------");
    
    //public String(byte[] bytes): convert byte array to string
    //Range of byte array: - 127 ~ + 128
    byte[] bys = {97, 98, 99, 100, 101};
    byte[] bys3 = {'a', 'b', 'c', 'd', 'e'};
    String s2 = new String(bys);
    String s8 = new String(bys3);
    System.out.println("s2:" + s2);//abcde; convert numbers to characters, then strings
    System.out.println("s8:" + s8);//abcde; convert numbers to characters, then strings
    System.out.println("s2.length():" + s2.length());
    System.out.println("-----------------------------------------------------------");
    
    //Public string (byte [] bytes, int offset, int length): convert part of byte array to string, starting from byte[offset], a total of length
    byte[] bys2 = {97, 98, 99, 100, 101, 102, 103};
    String s3 = new String(bys2,2,4);
    System.out.println("s3:" + s3);//cdef
    System.out.println("s3.length():" + s3.length());
    System.out.println("-----------------------------------------------------------");
    
    //public String(char[] value): convert character array to string
    char[] chs = {'a', 'b', 'c', 'd', 'e', 'f', 'love', 'Forest', 'young', 'Xia'};
    String s4 = new String(chs);
    System.out.println("s4:" + s4);//abcdef loves Lin Qingxia
    System.out.println("s4.length():" + s4.length());//10
    System.out.println("-----------------------------------------------------------");
    
    //public String(char[] value, int offset, int count): convert part of character array to character conversion
    char[] chs2 = {'a', 'b', 'c', 'd', 'e', 'f', 'love', 'Forest', 'young', 'Xia'};
    String s5 = new String(chs2,6,4);
    System.out.println("s5:" + s5);//Love Brigitte Lin
    System.out.println("s5.length():" + s5.length());//4
    System.out.println("-----------------------------------------------------------");
    
    //public String(String original): convert string constant to string
    String s6 = new String("abcde");
    System.out.println("s6:" + s6);//abcde
    System.out.println("s6.length():" + s6.length());//5
    System.out.println("-----------------------------------------------------------");
    
    //String literals, such as abc, can also be treated as an object
    String s7 = "abcde";
    System.out.println("s7:" + s7);//abcde
    System.out.println("s7.length():" + s7.length());//5
    
    
}

}

Posted by jess3333 on Thu, 14 Nov 2019 07:35:53 -0800