1

【笔记】将字符串放入串池

 1 year ago
source link: https://loli.fj.cn/2023/01/26/%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%94%BE%E5%85%A5%E4%B8%B2%E6%B1%A0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【笔记】将字符串放入串池

2023-01-26

将字符串放入串池学习笔记

字符串常量

  • new出来的字符串,编译器将自动将其放入串池
String s = "";
String s = new String("");

将堆中的字符串放入串池

  • 通过运算符得到的新字符串,编译器将自动将其放入堆中
  • 如果程序中使用了大量的重复字符串,可以通过将堆中的字符串放入串池,节省堆内存的使用,从而实现JVM调优
  • 可以通过intern()方法可以尝试将堆中的字符串放入串池,并返回串池中的对象
    • 如果已存在则不会再次放入串池
    • JDK1.8及之后,如果不存在则立即将堆中的字符串放入串池
    • JDK1.8之前,如果不存在则立即将堆中的字符串复制一份,将副本放入串池
String s = new String("") + new String("");
s = s.intern();

哔哩哔哩——黑马程序员


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK