23 String字符串

23.1 构造方法

23.1.1 无参构造方法

        创建一个空的字符串:

public class Demo1 {
    public static void main(String[] args) {
        String s = new String();
        System.out.println("s:"+s);
    }
}

23 String字符串_第1张图片

 23.1.2 有参构造方法(2个)

        23.1.2.1 根据字符数组创建字符串

public class Demo1 {
    public static void main(String[] args) {
        char[] arr = {'a','b','c'};
        String s = new String(arr);
        System.out.println("s:"+s);

    }
}

23 String字符串_第2张图片

23.1.2.2 根据字节数组创建字符串

        注意字节数组的97代表a,查ASCII即可。

public class Demo1 {
    public static void main(String[] args) {
        byte[] arr = {97,98,99};
        String s = new String(arr);
        System.out.println("s:"+s);

    }
}

23 String字符串_第3张图片

 

你可能感兴趣的:(java学习,java,开发语言)