您的位置:首页 > 编程语言 > Java开发

java中字符串的比较

2015-01-03 23:20 405 查看
public class StringCompareTest {
public static void main(String[] args){
String s1 = "Hello";
String s2 = "Hello";
// assert pass, because both s1 and s2 point to the same const object.
assert s1 == s2;

String s3 = new String("World!");
String s4 = new String("World!");
// AssertionError, because s3 and s4 are different object
// assert s3 == s4;
assert s3 != s4;
// assert pass, equals are used to compare the contents of two strings.
assert s3.equals(s4);

String s5 = new String("");
// AssertionError, don't examine an empty string with '=='
// assert s5 == "";

}
}

编译:
javac StringCompareTest.java

运行:
java -ea StringCompareTest
,
-ea
表示启用断言 (enable assertion),java虚拟机默认是关闭断言的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: