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 }
}字符串