赞
踩
如下代码,试问输出分别为____ _____?
- public class Test {
-
- public static void main(String[] args) {
- test();
- }
-
- public static void test(){
- String s = "hello";
- String t = "hello";
- char[] c = {'h','e','l','l','o'};
-
- System.out.println(s == t);
- System.out.println(s.equals(c));
- }
- }
答案: true false
重点分析“s == t”:
String s=”hello”; // 从字符串常量池中找“hello”,结果没有找到,创建了“hello”,引用s指向常量池地址
String t=”hello”; // 从字符串常量池中找“hello”,结果找到了,返回其引用(即s),因此引用t 和 s 指向同一地址,即 s == t
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。