当前位置:   article > 正文

牛客网题库分享--字符串常量池_s=new string(“hello”); string t =new string(“hello

s=new string(“hello”); string t =new string(“hello”); char c [ ] ={‘h’,’e’,’l’,’l’,’o’}; 下列哪些表达式返回true

如下代码,试问输出分别为____   _____?

  1. public class Test {
  2. public static void main(String[] args) {
  3. test();
  4. }
  5. public static void test(){
  6. String s = "hello";
  7. String t = "hello";
  8. char[] c = {'h','e','l','l','o'};
  9. System.out.println(s == t);
  10. System.out.println(s.equals(c));
  11. }
  12. }

答案: true   false

重点分析“s == t”:

String s=”hello”; // 从字符串常量池中找“hello”,结果没有找到,创建了“hello”,引用s指向常量池地址

String t=”hello”; // 从字符串常量池中找“hello”,结果找到了,返回其引用(即s),因此引用t 和 s 指向同一地址,即 s == t

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号