为什么第一个输出是Equal,而第二个是Not Equal public class TestString{String st= new String("Hello");public static void main(String arg[]){TestString test = new TestString();TestString1 test1 = new TestString1();if(test1.st1 == test1.st2)
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/09 05:51:34
为什么第一个输出是Equal,而第二个是Not Equal public class TestString{String st= new String("Hello");public static void main(String arg[]){TestString test = new TestString();TestString1 test1 = new TestString1();if(test1.st1 == test1.st2)
为什么第一个输出是Equal,而第二个是Not Equal
public class TestString
{
String st= new String("Hello");
public static void main(String arg[])
{
TestString test = new TestString();
TestString1 test1 = new TestString1();
if(test1.st1 == test1.st2)
System.out.println("Equal");
else
System.out.println("Not Equal");
if(test.st == test1.st1)
System.out.println("Equal");
else
System.out.println("Not Equal");
System.out.println(test.st+"\n"+test1.st1+"\n"+test1.st2);
}
}
class TestString1
{
String st1 = "Hello";
String st2 = "Hello";
}
为什么第一个输出是Equal,而第二个是Not Equal public class TestString{String st= new String("Hello");public static void main(String arg[]){TestString test = new TestString();TestString1 test1 = new TestString1();if(test1.st1 == test1.st2)
java中“==”比较的是引用变量的地址是否相等,可以用equals方法来比较,equals比较的是内容.