java设计者对String的优化

白色玫瑰 程序猿

时间: 2023-05-22 阅读: 1 字数:1458

{}
2.针对常量池的优化 当两个String对象拥有相同的值时,他们只引用常量池中的同一个对象拷贝。 String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc"); System.out.print...

1.不变性

当一个对象需要被多个线程共享,且访问频繁时,可以省略同步和锁等待的时间,从而大幅度提高系统性能。

2.针对常量池的优化

当两个String对象拥有相同的值时,他们只引用常量池中的同一个对象拷贝。

String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
System.out.println(str1 == str2);         // true
System.out.println(str1 == str3);         // false
System.out.println(str1 == str3.intern());  // true

intern()方法返回了String对象在常量池中的引用。

3.类的final定义。

原文地址:https://blog.csdn.net/qq_37769323/article/details/104190849?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168474998616800188596623%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168474998616800188596623&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-6-104190849-null-null.142^v87^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/2da50bc3db634145872997a9a1d43067

最新评论

当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号