当前位置: 技术问答>java相关
Compare with two strings
来源: 互联网 发布时间:2015-03-05
本文导语: When I run the test program as the following using javac Test testString I got the following results: test == testString test1 != testString test1 != test test2 == test test equal to testString test1 equals to testString test1 equals to test...
When I run the test program as the following using
javac Test testString
I got the following results:
test == testString
test1 != testString
test1 != test
test2 == test
test equal to testString
test1 equals to testString
test1 equals to test
test2 equals to test
So why test1 != testString and test1 != test ?
Thank you!
The following are my codes:
public static void main(String[] args) {
String test = "testString";
String test1 = args[2];
String test2 = test;
if (test == "testString") {
System.out.println("test == testString");
} else {
System.out.println("test != testString");
}
if (test1 == "testString") {
System.out.println("test1 == testString");
} else {
System.out.println("test1 != testString");
}
if (test1 == test) {
System.out.println("test1 == test");
} else {
System.out.println("test1 != test");
}
if (test2 == test) {
System.out.println("test2 == test");
} else {
System.out.println("test2 != test");
}
if (test .equals("testString")) {
System.out.println("test equal to testString");
} else {
System.out.println("test does not equal to testString");
}
if (test1 .equals("testString")) {
System.out.println("test1 equals to testString");
} else {
System.out.println("test1 does not equal to testString");
}
if (test1 .equals(test)) {
System.out.println("test1 equals to test");
} else {
System.out.println("test1 does not equal to test");
}
if (test2 .equals(test)) {
System.out.println("test2 equals to test");
} else {
System.out.println("test2 does not equal to test");
}
}
javac Test testString
I got the following results:
test == testString
test1 != testString
test1 != test
test2 == test
test equal to testString
test1 equals to testString
test1 equals to test
test2 equals to test
So why test1 != testString and test1 != test ?
Thank you!
The following are my codes:
public static void main(String[] args) {
String test = "testString";
String test1 = args[2];
String test2 = test;
if (test == "testString") {
System.out.println("test == testString");
} else {
System.out.println("test != testString");
}
if (test1 == "testString") {
System.out.println("test1 == testString");
} else {
System.out.println("test1 != testString");
}
if (test1 == test) {
System.out.println("test1 == test");
} else {
System.out.println("test1 != test");
}
if (test2 == test) {
System.out.println("test2 == test");
} else {
System.out.println("test2 != test");
}
if (test .equals("testString")) {
System.out.println("test equal to testString");
} else {
System.out.println("test does not equal to testString");
}
if (test1 .equals("testString")) {
System.out.println("test1 equals to testString");
} else {
System.out.println("test1 does not equal to testString");
}
if (test1 .equals(test)) {
System.out.println("test1 equals to test");
} else {
System.out.println("test1 does not equal to test");
}
if (test2 .equals(test)) {
System.out.println("test2 equals to test");
} else {
System.out.println("test2 does not equal to test");
}
}
|
当你在java程序中创建一个字符串常量时,虚拟机会生成一个字符串池来存放这些字符串对象,在上面的程序中,由于显式的对test字符串对象赋值(test="testString"),所以test对象的指针就是"testString"的指针,因此它们是相等的。而你判断test1和“testString”的结果不相等是因为test1的内容是arc[2]得来的,但是test1对象在内存处于一个新的位置,所以用"=="来判断肯定是返回false的。如果你把这条语句换成test1.equals("testString"),那结果就是true了。
请记住一点,在java中,“==”是判断对象在内存中的位置是否相等,而equals方法则是判断两个对象的属性/内容是否相等。
请记住一点,在java中,“==”是判断对象在内存中的位置是否相等,而equals方法则是判断两个对象的属性/内容是否相等。