字符串习题

public class Test
{
	public static void main(String args[])
	{
		String s1 = "This is a dog";
		String s2 = s1;
		String s3 = "this is a dog";
		String s4 = "This is a dog";
		System.out.println("s1.equals(s2):" + s1.equals(s2));
		System.out.println("s1.compareTo(s2):" + s1.compareTo(s2));
		System.out.println("s1.equals(s4):" + s1.equals(s4));
		System.out.println("s1.compareTo(s4):" + s1.compareTo(s4));
		System.out.println("s1.equals(s3):" + s1.equals(s3));
		System.out.println("s1.compareTo(s3):" + s1.compareTo(s3));	
		/*
			---------- 运行 ----------
			s1.equals(s2):true
			s1.compareTo(s2):0
			s1.equals(s4):true
			s1.compareTo(s4):0
			s1.equals(s3):false
			s1.compareTo(s3):-32
			输出完成 (耗时 0 秒) - 正常终止
		*/
	}
}

public class Test
{
	public static void main(String args[])
	{
		String s1 = "This ";
		String s2 = "is ";
		String s3 = "a ";
		String s4 = "String";
		String sConcat = s1.concat(s2).concat(s3).concat(s4);
		System.out.println(sConcat);
		/*
				---------- 运行 ----------
			This is a String

			输出完成 (耗时 0 秒) - 正常终止		
		*/
	}
}


	public static void main(String args[])
	{
		String s1 = "This ";
		String s2 = "is ";
		String s3 = "a ";
		String s4 = "String";
		String sConcat = s1.concat(s2).concat(s3).concat(s4);
		String sCopy = String.copyValueOf(s1.toCharArray());
		System.out.println("concat:" + sConcat + "\ncopyValueOf:" + sCopy);
		/*
			---------- 运行 ----------
			concat:This is a String
			copyValueOf:This 

			输出完成 (耗时 0 秒) - 正常终止
		*/
	}


posted @ 2015-01-04 15:36  静以养身 俭以养德  阅读(152)  评论(0编辑  收藏  举报