1. 用字符数组声明字符串变量
char ch[]= {'s','t','r','i','n','g'};
String str1=new String(ch);
2. 用字符数组的部分字符声明字符串变量
String str2=new String(ch,0,3);
3. 用字符串常量声明字符串变量
String str3="hello world";
示例:声明字符串
public class Demo{
public static void main(String[] args){
char ch[]= {'s','t','r','i','n','g'};
String str1=new String(ch);
String str2=new String(ch,0,3);
String str3="hello world";
System.out.println("str1="+str1);
System.out.println("str2="+str2);
System.out.println("str3="+str3);
}
}
输出结果:
str1=string
str2=str
str3=hello world