执行以下代码

  String a1=new String("abc");
       String a2=new String("abc");
       System.out.println(a1==a2);
       System.out.println(a1==a2.intern());
       System.out.println("abc"==a2.intern());
       System.out.println(a1.intern()==a2.intern());

返回

  false
  false
  true
  true

new String("abc") 方法是创建一个String对象, 同时引用常量池里的"abc", a1,a2分别是两块内存空间,a1!=a2; 而常量"abc"的引用是和a1,a2都不一样的, a1!=a2.intern(); "abc"是常量池的引用,和a2.intern()是指向同一个常量,所以"abc"==a2.intern()

JDK源码里有这么段说明

/**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();

再来看看下面这段代码

String a1="abc";
        String a2="abc";
        System.out.println(a1==a2);
        System.out.println(a1==a2.intern());
        System.out.println("abc"==a2.intern());
        System.out.println(a1.intern()==a2.intern());

全部返回true, a1,a2都是指向"abc"常量池

再来看看下面这段

  String a1="abc";
        String a2=new String("abc");
        System.out.println(a1==a2);
        System.out.println(a1==a2.intern());
        System.out.println("abc"==a2.intern());
        System.out.println(a1.intern()==a2.intern());

返回

  false
  true
  true
  true

PS: 相同字符串的intern()返回大部分情况是一样的, 因为存在调用了intern()方法后,该字符串被回收了,那么再次调用intern()会把相同的字符串加入到常量池里,但引用位置已经改变

String intern()方法详解的更多相关文章

  1. String的Intern方法详解

    引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念.常量池就类似一个JAVA系统级别提供的缓存.8种 ...

  2. String的intern()方法详解

    https://blog.csdn.net/soonfly/article/details/70147205  :图解 https://blog.csdn.net/wjzhang5514/articl ...

  3. String使用方法详解

    标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作 ...

  4. $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  5. 序列内置方法详解(string/list/tuple)

    一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...

  6. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  7. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  8. C++调用JAVA方法详解

    C++调用JAVA方法详解          博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...

  9. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

随机推荐

  1. 关于python课程的想法和建议。

    第一次听说python是在刚结束与世隔绝的高中生活之后,当时的网络上铺天遍地都是人工智能和机器学习,于是便知道了python这门编程语言.我是光电信息科学与工程专业的学生,这个专业的学生必须要懂计算机 ...

  2. Obtain older GMT versions

    ftp://ftp.soest.hawaii.edu/gmt/legacy/

  3. STL标准库-迭代器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函 ...

  4. anu - pureComponent

    import { inherit } from "./util"; import { Component } from "./Component"; impor ...

  5. jq 的onchange事件

    按商品类型显示:<select onchange="location.href='__ACTION__/type_id/'+this.value;">    <o ...

  6. Android输入法框架系统(上)

    输入法,就是用来输入字符(包括英文,俄文,中文)的工具.输入法你可以看成是一种字符发生器,它将输入数据触摸事件或者按键事件转化为其他更丰富的字符.在PC时代,输入法的原始输入来自实体键盘,鼠标,然后输 ...

  7. .Net Entity Framework Core 用 HasColumnType 配置浮点数精度

    一.前言 前段时间用.Net Entity Framework core搭建框架,需要配置浮点数的精度,发现.Net Entity Framework core 并没有HasPrecision方法.在 ...

  8. 移动端自动化openatx开源项目介绍,pytest并发测试框架结合

    开头 相信不少用过appium的同学,对于使用appium的一些体会与感受是否与我相似 1. appium启动服务和app程序非常慢 2. appium多线程并发需要启动多个服务 3. appium必 ...

  9. HDU 1565

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 先进行二分图黑白染色,S到黑,白到T,黑到白,问题转化成了求最大权独立集,最大点权独立集=sum-最小点权 ...

  10. 通过Servlet设置文件下载

    文件下载 1.获取要下载的文件的绝对路径 但是使用getServletContext().getRealPath()方法在不同的服务器上所获得的实现是不一样的 因为项目被打包入.war文件以后就失去了 ...