当前位置:网站首页>Intern method of string
Intern method of string
2022-06-30 04:22:00 【No bug program yuan】
String.intern() It's a local (native) Method , Its function is if the string constant pool already contains an equal to this String Object's string , Returns the String References to objects , Otherwise, a String Object to the string constant pool , And return a reference to this object , stay jdk6 Or before, the constant pool in the virtual machine is allocated to the permanent generation , We can go through -xx:PermSize and -xx:MaxPermSize Limit the size of the permanent generation , You can indirectly limit the capacity of its constant pool , stay jdk8 Metaspace is completely used in place of permanent generation .
Sample code :
public static void main(String[] args) {
String str=new StringBuilder("58").append("tongcheng").toString();
System.out.println(str);
System.out.println(str.intern());
System.out.println(str==str.intern());
System.out.println();
String str2=new StringBuilder("ja").append("va").toString();
System.out.println(str2);
System.out.println(str2.intern());
System.out.println(str2==str2.intern());
String s="java";
System.out.println(s==str2.intern());
}Execution results :
58tongcheng
58tongcheng
true
java
java
false
true Why? 58tongcheng yes true and java Of str2==str2.intern() yes false?
Because there is an initialized java character string (jdk Self contained ) In the load sum.misc.Version When this class enters the constant pool 
sun.misc.Version Class will be in JDK The class library is loaded and initialized during initialization , During initialization, it needs to set the static constant field according to the specified constant value (ConstantValue) Do default initialization , At this time sun.misc.Version.launcher Static constant field refers to "java" The string literal is intern To HotSpot VM String constant pool ——StringTable In the .
therefore new StringBuilder("ja").append("va").toString(); When is new One. , Print the first line str2 It was this when new String , The second sentence str2.intern() yes jdk Bring this with you launcher_name Variable java value , So print out false. Not the same String object
String s="java"; System.out.println(s==str2.intern()); by true,s It's the original jdk Loaded java character string
边栏推荐
- SQL追加字段
- Blue Bridge Cup: magic cube rotation [Vocational group]
- Machine learning notes
- Knowledge - how to build rapport in sales with 3 simple skills
- Myrpc version 2
- iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
- The same node code will cause duplicate data
- 基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
- Quick sort & merge sort
- Huawei cloud native - data development and datafactory
猜你喜欢

With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming

lego_loam 代码阅读与总结

Day 10 data saving and loading

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里

Redis sentry, persistence, master-slave, hand tear LRU

DBT product initial experience

Anonymous pipeline for interprocess communication

Huawei cloud native - data development and datafactory

Myrpc version 3

lego_ Reading and summary of loam code
随机推荐
Myrpc version 6
Error encountered in SQL statement, solve
FortiGate firewall configuration link detection link monitor and status query
基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
SQL server2005中SUM函数中条件筛选(IF)语法报错
OneNote production schedule
The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
Technology sharing | broadcast function design in integrated dispatching
Anonymous pipeline for interprocess communication
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
Qt 6.3.1Conan软件包发布
win10系统使用浏览器下载后,内容无故移动或删除
Unity 在编辑器中输入字符串时,转义字符的输入
If you encounter problems when using spark for the first time, please ask for help
Qt Creator 8 Beta2发布
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion
errno和perror
[learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
Detailed explanation of network layer