String intern()方法详解
执行以下代码
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™ 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()方法详解的更多相关文章
- String的Intern方法详解
引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念.常量池就类似一个JAVA系统级别提供的缓存.8种 ...
- String的intern()方法详解
https://blog.csdn.net/soonfly/article/details/70147205 :图解 https://blog.csdn.net/wjzhang5514/articl ...
- String使用方法详解
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作 ...
- $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...
- 序列内置方法详解(string/list/tuple)
一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
随机推荐
- git误提交了项目文件和配置文件的恢复方法
参考链接:https://my.oschina.net/yangfuhai/blog/708704
- 2019.1.7 Russia temperature control demo
1layout 2导出Gerber 做钢网 3刷锡膏 4.1调SMT程序: a摆元件,写P/N位置 b定位检测点 4.2手贴元件 手别抖! 5过炉 温度270 6插件PCBA 做载板最方便,手插焊接也 ...
- 一起来点React Native——常用组件之Image
一.前言 在开发中还有一个非常重要的组件Image,通过这个组件可以展示各种各样的图片,而且在React Native中该组件可以通过多种方式加载图片资源. 二.Image组件的基本用法 2.1 从当 ...
- Remastersys---制作当前的ubuntu系统镜像iso,自定义ubuntu系统
Remastersys---制作当前的ubuntu系统镜像iso,自定义ubuntu系统 Remastersys则款软件,我们就是使用他来将当前的系统制作成iso镜像,一方面可以用来备份系统,二来可以 ...
- 玩转TypeScript(3)--类型转换
使用强类型变量常常需要从一种类型向另一种类型转换,通常使用ToString或ParseInt可以来实现一些简单的转换,但是有时候需要像.NET语言中那样将一种类型显示的转换为另一种类型,在TypeSc ...
- magento的必备插件(转)
标签: 插件 magento magento插件 必备插件 Magento插件 ,插件,我要常见Magento必备插件 .. 都是免费的Magento插件,多而杂,乡亲们自己挑~~ [因收集而强大 & ...
- word2vec 小测试
Bag-of-words Model Previous state-of-the-art document representations were based on the bag-of-words ...
- chapter02 回归模型在''美国波士顿房价预测''问题中实践
#coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...
- 结构体内的函数与bfs的情景变量
关于结构体内的函数,太难的尚且不会用,下面是一个简单一点的结构体内函数的代码 定义这样一个结构体函数之后就能像如下这样使用了 以上为结构体内的简单函数,下面重点来了,关于bfs找最短路由于需要避免走回 ...
- MySQL Disk--SSD与RAID
===================================================SSD与RAID 51.在RAID 5这类Parity-RAID上存在partial-stripe ...