zoukankan      html  css  js  c++  java
  • Java

    intern()方法:把堆中的引用丢入常量池中,然后返回这个引用。当常量池中已经存在这个引用,就直接返回这个引用。(jdk1.8)

    由于jdk1.7中将字符串常量池改为存放在堆中,因此intern()方法的实现原理相对以前的版本也有所改变。

    我们根据jdk的版本来进行一些分析:

      jdk1.6中字符串常量池存放在永久代中:

        当使用intern()方法时,查询字符串常量池是否存在当前字符串,若不存在则将当前字符串复制到字符串常量池中,并返回字符串常量池中的引用。

      jdk1.7中字符串常量池存放在堆中:

        当使用intern()方法时,先查询字符串常量池是否存在当前字符串,若字符串常量池中不存在则再从堆中查询,然后存储并返回相关引用;

        若都不存在则将当前字符串复制到字符串常量池中,并返回字符串常量池中的引用。

    从上面叙述中,可以得出其中的区别:

      jdk1.6中只能查询或创建在字符串常量池;

      jdk1.7中会先查询字符串常量池,若没有又会到堆中再去查询并存储堆的引用,然后返回。

    例子:

     1 public class Test {
     2     public static void main(String[] args) {
     3         
     4             String s1 = new String("he") + new String("llo");//s1堆地址
     5             String s2 = new String("h") + new String("ello");//s2堆地址,s1!=s2
     6            
     7             //在常量池中找hello地址,没找到。
     8             //故将当前字符串的地址(s1堆的地址)复制到字符串常量池中,并返回字符串常量池中的引用,也就是s1的地址。
     9             String s3 = s1.intern(); //s3等于s1的堆地址
    10             
    11             //从字符串常量池中找,发现已经有hello地址,所以直接返回hello的地址,也就是s1
    12             String s4 = s2.intern(); //s4等于s1的堆地址
    13             
    14             System.out.println(s1 == s3);// true
    15             System.out.println(s1 == s4);// true
    16     }
    17 }

    更多例子与详情:https://www.cnblogs.com/aloenfs/p/9127353.html

  • 相关阅读:
    windows 10 上源码编译opengv | compile opengv on windows 10 from source
    ubuntu 16.04上源码编译libjpeg-turbo和使用教程 | compile and use libjpeg-turbo on ubuntu 16.04
    c 时间 学习
    cap文件的格式说明
    C#调用C++
    c/c++基础 const
    tomcat 简单安装配置说明
    言论
    近期任务
    解决mysql从windows迁移到centos出现乱码问题
  • 原文地址:https://www.cnblogs.com/linliquan/p/11259321.html
Copyright © 2011-2022 走看看