学习java第37天

1.String的创建方式

*直接初始化

 

public class StringDemo1{

  public static void main(String[] args){

    String name = "帅哥";

    System.out.println(name);

    }

}

*通过new关键字创建

public class StringDemo1{

  public static void main(String[] args){

   String name = new String("帅哥");

   System.out.println(name);

   }

}

2.String对象通过“+”串联

public class SyringDemo2 {

    public static void main(String[] args) {

       String s = "abc";

       s = s + 12;

       System.out.println(s);

    }

}

//abc12

 

从结果看,字符串相加还是字符串,

public class SyringDemo3 {

    public static void main(String[] args) {

       String s = "abc";

       s = s + 1+2+s;

       System.out.println(str);

    }

}

//3abc

1+2并不是字符串类型,是整型,整型相加再与字符串相连,就是这个结果

3.String类的本质是字符数组char[]

public class SyringDemo3 {

    public static void main(String[] args) {

       char[] ch = { 北','京','欢','迎','你' };

       char[] ch2 = { '张','三' };

       char chs = copy(ch, ch2);

       System.out.println(chs);

    }

    private static char[] copy(char[] ch, char[], ch2) {

       int len = ch.length + ch2.length;

       char[] chs = new char[len];

       for (int i = 0; i < ch.length; i++) {

           chs[i] = ch[i];

       }

       for (int i = 0; i < ch2.length; i++) {

           chs[ch.length + i] = ch2[i];

       }

       return chs;

    }

}

//北京欢迎你

//北京欢迎你,张三

4.明天学习内容:String池,hashCode和equals

.

 

posted @ 2020-08-11 20:27  鯨落  阅读(57)  评论(0编辑  收藏  举报