public class TestString
{
public static void main(String [] args)
{
String s1 = "hello";
String s2 = " world";
String s3 = "hello";
System.out.println(s1 ==s3);
s1 = new String ("hello");
s2 = new String("hello");
System.out.println(s1 ==s2);
System.out.println(s1.equals(s2));
char c [] = {'s','u','n',' ', 'j','a','v','a'};
String s4 = new String(c);
String s5 = new String(c,4,4);
System.out.println(s4);
System.out.println(s5);
}
}
写了一个Test小程序,主要目的是了解“==”和equals() 方法的区别,熟悉一下String类的常用构造方法,对“==”和 equals()方法还不是很明白,大家能给讲讲么。。。 |