本帖最后由 rover0321 于 2014-6-22 16:57 编辑
总结了一些关于String 对象引用与字符串常量比较的问题。
- public class StringTest
- {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- String a = "hello";
- String b = "hello";
- String newA = new String("hello");
- String c = "hel";
- String d = "lo";
- final String finalc = "hel";
- final String finalgetc = getc();
-
- System.out.println("a == b ? " + (a == b));
- System.out.println("a == newA ? " + (a == newA));
- System.out.println("a == hel + lo ? " + (a == "hel" + "lo"));
- System.out.println("c == finalc ? " + (a == finalc + "lo"));
- System.out.println("c == finalc ? " + (a == finalgetc + "lo"));
- System.out.println("a == c + d ? " + (a == c + d));
- System.out.println("finalgetc == finalc ? " + (finalgetc == finalc));
- }
-
- private static String getc()
- {
- return "hel";
- }
- }
复制代码
|
|