SummerVocation_Learning--String 类

    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:

public class TestString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = "hello",s2 = "world";
		String s3 = "hello";
		System.out.println(s1==s3); //判断s1,s3是否相等。输出true。
		
		s1 = new String("hello");
		s2 = new String("hello");
		System.out.println(s1==s2); //false
		System.out.println(s1.equals(s2)); //true
		
		char[] c= {'h','e','l','l','o',',','w','o','r','l','d'};
		String s4 = new String(c);
		String s5 = new String(c, 6, 5);
		System.out.println(s4); //hello,world
		System.out.println(s5); //world
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41684261/article/details/80992491
今日推荐