String类的构造方法

常见的构造方法
在这里插入图片描述
案例演示

public class StringGouZao {
    public static void main(String[] args) {
        String s1 = new String ( );   //空构造
        System.out.println (s1);

        byte[] arr1 = {97 , 98 , 99};
        String s2 = new String (arr1);  //将字节数转化成字符串
        System.out.println (s2);

        byte[] arr2 = {97 , 98 , 99 , 100 , 101};
        String s3 = new String (arr2 , 2 , 3);  //把字节数组的一部分转化成字符串
        System.out.println (s3);

        char[] arr3 = {97 , 98 , 99 , 100 , 101};
        String s4 = new String (arr3);  //把字符数组转化成字符串
        System.out.println (s4);

        char[] arr4 = {97 , 98 , 99 , 100 , 101};
        String s5 = new String (arr4 , 2 , 2);  //把字符数组的一部分转化成字符串
        System.out.println (s5);

        String s6 = new String ("hello");   //把字符常量值转化成字符串
        System.out.println (s6);
    }
}

发布了55 篇原创文章 · 获赞 5 · 访问量 4181

猜你喜欢

转载自blog.csdn.net/qq_43654669/article/details/97145358