package cn.itcast_01;數組
/*code
A: 字符串字面值,如abc,也能夠看作一個對象
B:字符串是常量,通常被賦值,就不能被修改
public String():空構造
public String(byte[] bytes):把字節數組轉換成字符串
public String(byte[] bytes ,int offset,int length):把字節數組的一部分轉換成字符串
public String(char[] value):把字符數組轉換成字符串
public String(char[] value, int offset, int count):把字符數組的一部分轉換成字符轉
public String(String original):把字符串常量轉換成字符串
public int length():返回此字符串的長度
public class StringDemo {對象
public static void main(String[] args) { //public String():空構造 String s1 = new String(); System.out.println("S1:" + s1);//複習上一節課:直接輸出一個對象,輸出的結果是該對象的地址值;可是存在重寫,因此不會輸出地址值 System.out.println("s1.length():" + s1.length()); System.out.println("-----------------------------------------------------------"); //public String(byte[] bytes):把字節數組轉換成字符串 //字節數組的範圍:-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;先把數字轉換成字符,再轉換成字符串 System.out.println("s8:" + s8);//abcde;先把數字轉換成字符,再轉換成字符串 System.out.println("s2.length():" + s2.length()); System.out.println("-----------------------------------------------------------"); //public String(byte[] bytes ,int offset,int length):把字節數組的一部分轉換成字符串,從byte[offset]開始,共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):把字符數組轉換成字符串 char[] chs = {'a', 'b', 'c', 'd', 'e', 'f', '愛', '林', '青', '霞'}; String s4 = new String(chs); System.out.println("s4:" + s4);//abcdef愛林青霞 System.out.println("s4.length():" + s4.length());//10 System.out.println("-----------------------------------------------------------"); //public String(char[] value, int offset, int count):把字符數組的一部分轉換成字符轉 char[] chs2 = {'a', 'b', 'c', 'd', 'e', 'f', '愛', '林', '青', '霞'}; String s5 = new String(chs2,6,4); System.out.println("s5:" + s5);//愛林青霞 System.out.println("s5.length():" + s5.length());//4 System.out.println("-----------------------------------------------------------"); //public String(String original):把字符串常量轉換成字符串 String s6 = new String("abcde"); System.out.println("s6:" + s6);//abcde System.out.println("s6.length():" + s6.length());//5 System.out.println("-----------------------------------------------------------"); //字符串字面值,如abc,也能夠看作一個對象 String s7 = "abcde"; System.out.println("s7:" + s7);//abcde System.out.println("s7.length():" + s7.length());//5 }
}字符串