理解String的intern()方法

API文档中的介绍:

intern

public String intern()

Returns a canonical representation for the string object.

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 ts.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对象,若不存在,则将该对象加入常量池,并返回该对象的引用。
代码验证如下:

1 public static void main(String[] args){
2         String a="abc";
3         String b=new String("abc");
4         String c=b.intern();
5         System.out.println(b==a);
6         System.out.println(b==c);
7         System.out.println(c==a);
8     }

输出结果为:

1 false
2 false
3 true

时间: 2024-08-04 17:30:13

理解String的intern()方法的相关文章

java String 中 intern方法的概念

1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. new String()和new String(“”)都是申明一个新的空字符串,是空串不是null: 3. String str=”kvill”:String str=new String (“kvill”);的区别: 在这里,我们不谈堆,也不谈栈,只先简单引入常量池这个简单的概念. 常量池(const

String的intern方法解析

public String intern()返回字符串对象的规范化表示形式. 一个初始时为空的字符串池,它由类 String 私有地维护. 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串.否则,将此 String 对象添加到池中,并且返回此 String 对象的引用. 它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.

String中intern方法的作用

前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池存放于方法区中 2.jdk1.6 方法区放在永久代(java堆的一部分),jdk1.7 特别将字符串常量池移动到了的堆内存中(使用参数-XX:PermSize 和-XX:MaxPermSize指定大小),jdk1.8放在单独的元空间里面(-XX:MaxMetaspaceSzie设定大小),和堆相独立

java中String类intern()方法探索

想到什么写什么,会有点乱,意思理解就行 首先我们创建两个字符串对象,如下: String a = new String("hx"); String b = new String("h")+new String("x"); 通过new关键字创建字符串对象时,会同时在堆和常量池中生成两个对象,比如说上面的第一条语句产生两个内容均为"hx"的对象,一个在堆中,一个在常量池中,a引用的是堆中的对象,内容是"hx";

String的Intern方法详解

引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念.常量池就类似一个JAVA系统级别提供的缓存.8种基本类型的常量池都是系统协调的,String类型的常量池比较特殊.它的主要使用方法有两种: 直接使用双引号声明出来的String对象会直接存储在常量池中. 如果不是用双引号声明的String对象,可以使用String提供的intern方法.intern 方法会从字符串常量池中查询当前字符串是否存在,

Java String的intern方法

在Java6中,intern方法返回的是对应字符串在永久态(方法区)中的地址:Java7以后,intern方法返回的是该字符串首次创建时候的堆内存的地址: 在java7中: package com.ecarx.daa.data.manager.utils; public class StringTest { public static void main(String[] args) { String a = new StringBuffer("a").append("a&q

jdk1.6与jdk1.8中String的intern()方法区别

我们用如下代码来证明两个版本intern方法的区别. jdk1.6 String ab = new String("a") + new String("b"); //在堆中创建"a","b","ab"; String ab2 = ab.intern();//先判断常量池中是否有"ab",如果有直接返回常量池中的地址,如果没有则拷贝一份ab对象放入常量池. String ab3 = &qu

String的intern方法

(jdk 7 以上) String s = new String("a") + new String("b"); s.intern(); 执行 s.intern() 时,如果字符串 ab 已经在常量池中,则直接返回: 如果不存在,会把当前引用放到常量池,该引用指向着 s 指向的堆中的对象. 所以: // 例1 String s = new String("a") + new String("b"); s.intern(); S

Java技术——你真的了解String类的intern()方法吗

0.引言 转载请注明出处:http://write.blog.csdn.net/postedit/52291082 什么都先不说,先看下面这个引入的例子: String str1 = new String("SEU")+ new String("Calvin"); System.out.println(str1.intern() == str1); System.out.println(str1 == "SEUCalvin"); 本人JDK版本1