理解String的intern()方法
API文档中的介绍:
intern
public String intern()
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object)method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.
- Returns:
- a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
- 意思就是说:
- 该方法返回一个规范化表示的string对象。
- 一个字符串常量池,初始化为空,且被String类私有地维护。
- 当intern()被调用时,如果常量池中存在和该String对象equal的对象(由equals方法决定),则返回常量池中已有的String对象,若不存在,则将该对象加入常量池,并返回该对象的引用。
- 代码验证如下:
-
public static void main(String[] args){
String a="abc";
String b=new String("abc");
String c=b.intern();
System.out.println(b==a);
System.out.println(b==c);
System.out.println(c==a);
}输出结果为:
false
false
true
理解String的intern()方法的更多相关文章
- java String 中 intern方法的概念
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- String的intern方法的使用场景
在讲intern方法前,我们先简单回顾下Java中常量池的分类. 常量池的分类 Java中常量池可以分为Class常量池.运行时常量池和字符串常量池. 1. Class文件常量池 在Class文件中除 ...
- String 的intern() 方法说明
1.说明 Java中string.intern()方法调用会先去字符串常量池中查找相应的字符串,如果字符串不存在,就会在字符串常量池中创建该字符串然后再返回. 2.源码说明 public native ...
- String 的 intern() 方法解析
一.概述 JDK7 之前和之后的版本,String 的 intern() 方法在实现上存在差异,本文的说明环境是 JDK8,会在文末说明 intern() 方法的版本差异性. intern() 方法是 ...
- String中intern方法的作用
前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池 ...
- Java - 记录String中intern()方法的学习与理解
intern()方法:把堆中的引用丢入常量池中,然后返回这个引用.当常量池中已经存在这个引用,就直接返回这个引用.(jdk1.8) 由于jdk1.7中将字符串常量池改为存放在堆中,因此intern() ...
- 关于对String中intern方法的理解
在java的String中有个一直被我们忽视了的方法intern方法:它的官方解释是:一个初始时为空的字符串池,它由类 String 私有地维护. 当调用 intern 方法时,如果池已经包含一个等于 ...
- String的intern方法的用处
今天第一次翻看Effective java,在其第一个item中讲静态工厂方法的有点的时候说到“它们每次被调用 的时候,不要非得创建一个新的对象”并在结尾处提到---"String.inte ...
- 从Java的字符串池、常量池理解String的intern()
前言 逛知乎遇到一个刚学Java就会接触的字符串比较问题: 通常,根据"==比较的是地址,equals比较的是值"介个定理就能得到结果.但是String有些特殊,通过new Str ...
随机推荐
- flutter之VSCode下Flutter常用终端命令行
https://www.cnblogs.com/lxlx1798/p/11049922.html 梁飞宇 [Flutter学习]之VSCode下Flutter常用终端命令行 Flutter 常用命令行 ...
- leetcode303 Range Sum Query - Immutable
""" Given an integer array nums, find the sum of the elements between indices i and j ...
- python实现二分法
前言: 二分法主要是用来查找位置的id,每次能够排除掉一半的数据,查找的效率非常高,但是局限性比较大. 必须是有序序列才可以使用二分查找. 原理 首先,假设表中元素是按升序排列,将表中间位置记录的关键 ...
- C语言中可变参数的原理——printf()函数
函数原型: int printf(const char *format[,argument]...) 返 回 值: 成功则返回实际输出的字符数,失败返回-1. 函数说明: 使用过C语言的人所再熟悉不过 ...
- pythonfile的知识点
1. file=open("/test/case1.txt","w") #open(路径+文件名,读写模式) #读写模式: r:只读(默认) rb:读二进制文件 ...
- P 1008 数组元素循环右移问题
转跳点:
- Day4-T3
原题目 吉儿是一家古董店的老板娘,由于她经营有道,小店开得红红火火.昨天,吉儿无意之中得到了散 落民间几百年的珍宝——月亮之眼.吉儿深知“月亮之眼”价值连城:它是由许多珍珠相连而成的,工 匠们用金线连 ...
- 098-PHP二维数组的元素输出
<?php $stu=array(array(76,87,68), array(65,89,95), array(90,80,66), array(90,95,65)); //定义一个二维数组 ...
- Docker部署Python应用程序
Docker部署Python应用程序 1. 单个py文件部署 生成Dockerfile 文件 插件用的豆瓣的镜像,,重置时间(容器的默认时间是UTC时间与宿主机的相差8小时). 文中需要三个插件(pe ...
- 导出execl
string filepath = Utils.GetMapPath("/upload/excel/"); filepath = filepath + fileName + &qu ...