java中String属于java.lang的package包,是一个类。代表不可变的字符序列。
String类的常见构造方法:
String(String original),创建一个对象为original的拷贝。
String(char[] value),用一个字符数组创建一个String对象。
String(char[] value,int offset,int count),用一个数组从offset项开始的count个字符序列创建一个String对象。
举例1:
1 public class TestString { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 String s1 = "hello",s2 = "world"; 6 String s3 = "hello"; 7 System.out.println(s1==s3); //判断s1,s3是否相等。输出true。 8 9 s1 = new String("hello"); 10 s2 = new String("hello"); 11 System.out.println(s1==s2); //false 12 System.out.println(s1.equals(s2)); //true 13 14 char[] c= {'h','e','l','l','o',',','w','o','r','l','d'}; 15 String s4 = new String(c); 16 String s5 = new String(c, 6, 5); 17 System.out.println(s4); //hello,world 18 System.out.println(s5); //world 19 } 20 21 }