文字列オブジェクトを作成する6つの方法(java)
1485 ワード
package javaBasic;
public class StringConstruction {
public static void main(String[] args) {
String str0 = new String("HelloJava");//0.String(String original) String , ; , 。
char chars1[] = {'j','a','v','a'};
char chars2[] = {'H','e','l','l','o','j','a','v','a'};
String str11 = new String(chars1);//①String(char[] value) String , 。
String str12 = new String(chars2,5,4);
/*②String(char[] value, int offset, int count) String , 。
str2*/
byte asc1[] = {106,97,118,97};
byte asc2[] = {72,101,108,108,111,106,97,118,97};
String str21 = new String(asc1);//③String(byte[] bytes) String 。
String str22 = new String(asc2,5,4);//④String(byte[] bytes, int offset, int length) String 。
String str31 = new String();//String() String , 。
StringBuffer sb = new StringBuffer("HelloJava");
String str32 = new String(sb);
System.out.println("str0:"+str0);
System.out.println("str11:"+str11);
System.out.println("str12:"+str12);
System.out.println("str21:"+str21);
System.out.println("str22:"+str22);
System.out.println("str31:"+str31);
System.out.println("str32:"+str32);
}
}