一道关于字符串常见的面试题:

下面代码在内存中创建了几个对象:

String str = "hello";
String str2 = new String(str);

这段代码就创建了2个String对象,str指向在常量池中的,str2指向堆上的,而str内部的char value[]则指向常量池中的char value[],所以这里的答案是2个对象。

可以通过Debug调试查看验证:

Java中 String str = new String("hello"); 到底创建了几个对象

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
猜你喜欢
  • 2021-09-12
  • 2022-12-23
  • 2021-08-26
  • 2021-07-28
  • 2021-12-26
  • 2021-05-01
  • 2022-12-23
相关资源
相似解决方案