String 与 Stringbuffer分析和总结.docxVIP

  • 3
  • 0
  • 约1.25万字
  • 约 11页
  • 2023-06-09 发布于上海
  • 举报
String 与 Stringbuffer. 2007 年 06 月 08 日 星期五 11:33 String 的创建 String s = hello; JVM 先根据内容hello查找对象,如果没有找到,则在 heap 上创建新对象,并将其赋予s1,否则使用已经存在的对象 String s = new String(hello); JVM 直接在 heap 上创建新的对象,所以在 heap 中会出现内容相同,地址不同的 String 对象 String 的比较 == 比较地址 equals 比较内容 举例: String s1 = hello; String s2 = hello; String s3 = new String(hello); s1 == s2; // true 地址相同 s1 == s3; // false 地址不同 s1.equals(s2); // true 内容相同 s1.equals(s3); // true 内容相同 intern() 方法 查找内容相同(equals())的字符串 String s1 = hello; // hello 不存在,jvm 创建新对象 (1) String s2 = new String(hello); // 创举新对象 (2),这时 heap 中存在两个内容为 hello 的对象 s1 == s2; s1.equal

文档评论(0)

1亿VIP精品文档

相关文档