Java String pool 问题。 Stringmy name is jack="jack";//条件是String pool没有"jack"这个对象,这行执行了什么?

What is String pool in Java? - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
This question already has an answer here:
I am confused about StringPool in Java. I came across this while reading the String chapter in Java. Please help me understand, in layman terms, what StringPool actually does.
105k22131195
marked as duplicate by , , , ,
This question has been asked before and already has an answer. If those answers do not fully address your question, please .
This prints true (even though we don't use equals method: correct way to compare strings)
String s = "a" + "bc";
String t = "ab" + "c";
System.out.println(s == t);
When compiler optimizes your string literals, it sees that both s and t have same value and thus you need only one string object. It's safe because String is immutable in Java.
As result, both s and t point to the same object and some little memory saved.
Name 'string pool' comes from the idea that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.
46.4k14115153
I don't think it actually does much, it looks like it's just a cache for string literals.
If you have multiple Strings who's values are the same, they'll all point to the same string literal in the string pool.
String s1 = "Arul"; //case 1
String s2 = "Arul"; //case 2
In case 1, literal s1 is created newly and kept in the pool. But in case 2, literal s2 refer the s1, it will not create new one instead.
if(s1 == s2) System.out.println("equal"); //Prints equal.
String n1 = new String("Arul");
String n2 = new String("Arul");
if(n1 == n2) System.out.println("equal"); //No output.
3,27722140
Let's start with a quote from the virtual machine spec:
Loading of a class or interface that contains a String literal may create a new String object (§2.4.8) to represent that literal. This may not occur if the a String object has already been created to represent a previous occurrence of that literal, or if the String.intern method has been invoked on a String object representing the same string as the literal.
This may not occur - This is a hint, that there's something special about String objects. Usually, invoking a constructor will always create a new instance of the class. This is not the case with Strings, especially when String objects are 'created' with literals. Those Strings are stored in a global store (pool) - or at least the references are kept in a pool, and whenever a new instance of an already known Strings is needed, the vm returns a reference to the object from the pool. In pseudo code, it may go like that:
1: a := "one"
--& if(pool[hash("one")] == null)
pool[hash("one") --& "one"]
return pool[hash("one")]
2: b := "one"
--& if(pool[hash("one")] == null)
// false, "one" already in pool
pool[hash("one") --& "one"]
return pool[hash("one")]
So in this case, variables a and b hold references to the same object. IN this case, we have (a == b) && (a.equals(b)) == true.
This is not the case if we use the constructor:
1: a := "one"
2: b := new String("one")
Again, "one" is created on the pool but then we create a new instance from the same literal, and in this case, it leads to (a == b) && (a.equals(b)) == false
So why do we have a String pool? Strings and especially String literals are widely used in typical Java code. And they are immutable. And being immutable allowed to cache String to save memory and increase performance (less effort for creation, less garbage to be collected).
As programmers we don't have to care much about the String pool, as long as we keep in mind:
(a == b) && (a.equals(b)) may be true or false (always use equals to compare Strings)
Don't use reflection to change the backing char[] of a String (as you don't know who is actualling using that String)
78.3k8107191
When the JVM loads classes, or otherwise sees a literal string, or some code interns a string, it adds the string to a mostly-hidden lookup table that has one copy of each such string.
If another copy is added, the runtime arranges it so that all the literals refer to the same string object.
This is called "interning".
If you say something like
String s = "test";
return (s == "test");
it'll return true, because the first and second "test" are actually the same object.
Comparing interned strings this way can be much, much faster than String.equals, as there's a single reference comparison rather than a bunch of char comparisons.
You can add a string to the pool by calling String.intern(), which will give you back the pooled version of the string (which could be the same string you're interning, but you'd be crazy to rely on that -- you often can't be sure exactly what code has been loaded and run up til now and interned the same string).
The pooled version (the string returned from intern) will be equal to any identical literal.
For example:
String s1 = "test";
String s2 = new String("test");
// "new String" guarantees a different object
System.out.println(s1 == s2);
// should print "false"
s2 = s2.intern();
System.out.println(s1 == s2);
// should print "true"
53.4k1079122
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabledjava中string的缓冲池_百度知道
java中string的缓冲池
提问者采纳
同理;hello& + & + &hello&quot.每一个内容相同的字符串对象都对应于一个 pool 里的对象,s1 当然还是引用 .println(s1 == s2;。通俗一点说,其实和第一条是一样的.很显然.所以三个都返回 false.所以这里的结果是 false总结一下就是,则在 Pool 创建这个对象.这样就很容易理解了,两边的值是引用值或者堆里的字符串对象).intern();System.即 在 pool 里查找有没&quot. ;abc&quot.按照 jdk 的帮助文档来说;abc&).其中 s2;hel&quot,这个会返回 pool 中的&quot.很明显是不同的对象;); + &lo&abc&.println(s == s1.,所以不用在 pool 中再次创建&求输出的结果解答 true false首先;abc&hello&quot.out.(注意.out,并返回这个对象.intern()),就是返回对应这个字符串内容的那个 pool 里的对象.;,首先括号里的&).s1 与 s2.intern());abc&quot,上面已经说明了.println(s == s1);abc&quot.只有 new 生成的才是对象;请问 前面三条语句分别创建了几个对象.1 看下面一段代码,String 对象一旦创建就不允许修改String 类有一个对应的 String 池;abc&quot,虽然内容都是&&quot:3 第三个问题String hello = &hello&quot,但是也不是相同的对象;S这个对象.注意。因为第一条已经在 pool 中创建了&String s1 = &quot.最后的答案是 false false trueintern()方法.那么将不会再 pool 里查找&对象.所以.s 与s2;abc&这个对象; 按照内容来说. 这条语句;;abc& 返回的是 true ,一个指向 pool 里的.String 类是一个不可变的类.intern()返回的是 pool 中的&quot.intern().println(hello == &abc&abc&quot.这里的 s 指向堆里的对象;对象;System,所以这条语句由于在 pool 中先找到了&Sabc&quot,第一条语句在 pool 里已经创建了一个&quot.所以返回的是;.out,返回 false.下面的三个==判断;).如果加号两边不满足上面的条件(比如,分别是什么,并返回 pool 里的对象;lo&quot.s1.;了,那可以拿具体例子来说s1.这样说也许还看不太明白.String hel = &quot.后面的&abc&quot.没有则创建一个;对象,而只是在堆里创建了一个新的&quot.;.也不是相同的对象;hel&quot..后面的输出分别是什么(1)String s = new String(&);;,没有则在 pool 里创建这个对象.,则返回 pool 里的对象;对象.这个时候;;System.现在我们考虑&quot.2 第二个问题String s = new String(&quot.,返回 false,那么将在 pool 里查找有没对应内容的对象(这里的内容就是&quot.;hello&System.上面说了.;abc&hel&quot,但是它将在堆里面生成一个&hel&quot,比如上面的&SString lo = &quot.intern()).out.out.注意,而是直接在堆里生成一个新的对象;abc&),并返回.其内容都是&quot,s1 指向的是 pool 中的&String s2 = new String(&quot.其实就是上面括号里的&hello&quot.println(hello == &Slo&abc&).第三个;abc&quot.intern 返回的是 pool 里的&quot,一个指向堆;abc&quot.而&abc&abc&lo&;abc&quot,而 s1 也是指向 pool 中的&;对象;这个对象;里写的值;先到 String pool 里看有没&,在 pool 里去查找 s1 对应的内容(也就是&quot,s 不是对象;).println(s == s2.如果没有(老实说;hel&String s1 = &quot,s 与 s2;abc&quot.执行的是相同的操作;).创建的流程是..;与&quot.所以这条语句没有创建对象,内容也是&quot.println(s == s2).;,一个指向堆里的对象,只是引用;abc&quot.所以这里就在 pool 创建了一个&hello&quot.;abc&quot,创建了两个对象,s1 与s2.然后 通过new 语句又创建了一个&abc&quot,虽然都是指向堆里的对象; 这条语句;对象; + lo)..一个指向 pool,hello == &对象.s2 指向的是堆里的&对象;abc&.也就说;求最后输出是什么解答,hello hel lo 这三个都是指向 pool 中的对象;abc&.这和 hello 是一样的,我没想到有哪种情况是没有的);lo& 这句,如果加号两边的是字面值(字面值就是直接在&abc&abc&quot..与 s 这个堆里的对象肯定不同.他的执行流程是;hel&quot.(2) String s1 = &quot,s 与 s2 表示的是不同的对象(4)接下来就很好说了,是返回字符串对象的规范化表示形式;hel&); + lo 虽然内容也是&,两个相加也就是&quot.如果找到.String s = new String(&quot.intern()也肯定不同;String s2 = new String(&quot,==永远是判断内存地址是否相等) s 与 s1.abc&quot.println(s1 == s2),也就是 S(3)String s2 = new String(&quot.java 中 String 创建详细解释字符串对象是一种特殊的对象.而这个对象是放在内存的堆里
提问者评价
很满意 基本数据类型用==比较2个值是否相同, 是比较他们在栈里面的地址吗?
其他类似问题
为您推荐:
其他1条回答
java语言使用层面上说a就是一个String对象;,以及各种池(静态池,真正的对象是&quot,a是指向&quot,比较详细的解释了java中对象的产生过程;SS&的地址,它存放在堆里,sun官方出的,这可以通过 a instanceof String来判断 从jvm实现层面上说,是个栈上变量 a不需要实例化直接使用那是a未初始化时是null有空可以去买java核心思想来看;SS&quot
string的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁在Java语言中 String str=new String(&a&) 这个语句创建了几个对象。_百度知道
在Java语言中 String str=new String(&a&) 这个语句创建了几个对象。
的区别:该语句在进行String类的构造函数调用时引用的参数&a&quot,若无则创建一个&quot,再将该对象的地址引用传递给对象Sa&quot,所以说创建了两个对象;)和String str=&对象;a&quot答案是,他们都在详细的讨论String str=new String(&quot?我看了好几个朋友写的帖子:2个对象解释,我的理解正确吗。请各位高手帮忙看一下,所以不是很明白;a&quot,查询内存中有无该对象;时,存于栈中
直接给它初始化;a&quot。这两个的结果是一样的,然后是指向str的引用,一个在堆里,也算是基本的数据类型,一个在栈里,用了new关键字在堆里创建了一个字符串对象,但是存储的过程是不同的,但是他比较特殊String类型是引用型的数据类型。String str=new String(&a&是把string当基本类型的使用;)说明他是引用型的的。String str=&quot,对于学习后面的数据操作就可以看出来了
那我的理解也就是对的了啊?我找了查了好多人的帖子和资料,最后感觉自己有点晕了。
可以这样理解
其他类似问题
为您推荐:
其他4条回答
) 创建两个对象是正确的,在堆中创建一个字符串常量池,当执行该语句时,系统会在栈中创建一个对象,然后在里面寻找是否有“a“,如果有则通过栈中的对象引用此值String str=new String(&a&quot
答案:两个,一个是字符串字面量&xyz&所对应的、驻留(intern)在一个全局共享的字符串常量池中的实例,另一个是通过new String(String)创建并初始化的、内容与&xyz&相同的实例
这是根据Java语言规范相关规定可以给出的合理答案。考虑到Java语言规范中明确指出了:
The Java Language Specification, Third Edition 写道
The Java programming language is normally compiled to the bytecoded instruction set and binary format defined in The Java Virtual Machine Specification, Second Edition (Addison-Wesley, 1999).
也就是规定了Java语言一般是编译为Java虚拟机规范所定义的Class文件,但并没有规定“一定”(must),留有不使用JVM来实现Java语言的余地。
考虑上Java虚拟机规范,确实在...
大哥,你这个是复制的哪的啊,看了半天累死了还没看怎么懂。
这个要看是在什么情况下 在编译阶段和运行阶段是不同的 都给你提示道这个程度了
剩下的你懂的哦(内存堆栈)
一个引用 一个对象String str=&a & 在池中找
java语言的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁java String字符串的比较问题_百度知道
java String字符串的比较问题
String b=&quot.println(s1==s3);abc&;b&quot.println(s1==s2);+&quot.out.println(s1==s3).;
String s2=&c&c&quot, 而S
String s1=&
String s3=a+b+c;a&.out.println(s1==s2);结果为+&
Spublic class Test1{ public static void main(String args[]) {
String a=&quot.println(s3);b&.out。请指教;a&quot. String c=&quot
我有更好的答案
1.需要引入字符串池的概念(String pool),但要创建字符串对象的时候,首先会搜索字符串池是否已经存在,如果存在则不创建。
2.==是比较对象的引用,也就是对应内存的地址是否一样。
3.这样的问题,在通常的开发中不会应用,但会出现在面试中。
4.s1和s2引用了同一个地址,所以是true。而s3是由三个字符串对象创建新的字符串,因此不是同一个对像,为false。
5.要对字符串的intern()进行了解及 The Java Language Specification
的3.10.5 String Literals。参考jdk api。package testP
class Test {
public static void main(String[] args) {
String hello = &Hello&, lo = &lo&;
System.out.print((hello == &Hello&) + & &);
System.out.print((Other.hello == hello) + & &);
与String池有关:java 虚拟机中本身含有了一些字符了。如果没有某字符串,才创建个字符串。这样一来就可以多个变量去引用某个字符串了。s1 == s3 为false是因为s3 指向 a + b + c 变量的运算结果,未来节省空间。会创建1个StringBuffer 来存放数据。最后toString()返回字符串,是新分配的。
s1 和 s2 是引用了栈内存中的&abc&字符串 所以是同一个对象s3则是经过a b c三个对象的计算产生了新的对象在堆内存中,而==操作符是比较两边的对象是否是同一个对象。
个人推测:右边是双引号的情况下,String对象声明并初始化的时候返回的是同一个对象,所以s1==s2是成立的;右边是对象的情况下,String对象声明并初始化的时候返回的是新的对象,所以s1==s3不成立。建议String比较的话使用equals方法s1.equals(s3)
==比较的是两边的对象是否是同一个对象 并且值相等equals比较值s3是 新产生的对象和s1不等所以false而System.out.println(s1.equals(s3));值为true
字符串比较不能用&==&而是用 equals()方法。 比如 s1.equals(s2);
这就是=和equals的区别.这个东西一句二句说不清楚,网上资料非常多.自己去看看吧.
您可能关注的推广
string的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁3759人阅读
字符串对象是一种特殊的对象.String类是一个不可变的类..也就说,String对象一旦创建就不允许修改
String类有一个对应的String池,也就是 String pool.每一个内容相同的字符串对象都对应于一个pool里的对象.
1 看下面一段代码.
String s = new String(&abc&);
&&String s1 = &abc&;
&&String s2 = new String(&abc&);
&&System.out.println(s == s1);
&&System.out.println(s == s2);
&&System.out.println(s1 == s2);
请问 前面三条语句分别创建了几个对象,分别是什么.后面的输出分别是什么
(1)String s = new String(&abc&); 这句,创建了两个对象..其内容都是&abc&.注意,s不是对象,只是引用.只有new生成的才是对象.
创建的流程是,首先括号里的&abc&先到String pool里看有没&abc&这个对象,没有则在pool里创建这个对象..所以这里就在pool创建了一个&abc&对象.然后 通过new语句又创建了一个&abc&对象..而这个对象是放在内存的堆里. .这里的s指向堆里的对象.
(2)&String s1 = &abc&; 这条语句,s1当然还是引用.没啥可说的.后面的&abc&.其实就是上面括号里的&abc&.执行的是相同的操作.即 在pool里查找有没&abc&这个对象.没有则创建一个...很显然,第一条语句在pool里已经创建了一个&abc&.所以这条语句没有创建对象,s1指向的是pool中的&abc&
(3)String s2 = new String(&abc&); 这条语句,其实和第一条是一样的,但是,因为第一条已经在pool中创建了&abc&这个对象,所以,这条语句创建了一个对象.s2指向的是堆里的&abc&.注意,虽然内容都是&abc&,s与s2表示的是不同的对象
(4)接下来就很好说了.下面的三个==判断.(注意,==永远是判断内存地址是否相等) s与s1,一个指向堆里的对象,一个指向pool里的.很明显是不同的对象.s与s2.上面说了,虽然都是指向堆里的对象,内容也是&abc&,但是也不是相同的对象.s1与s2.一个指向pool,一个指向堆.也不是相同的对象.所以三个都返回false.
2 第二个问题
String s = new String(&abc&);
&&String s1 = &abc&;
&&String s2 = new String(&abc&);
&&System.out.println(s == s1.intern());
&&System.out.println(s == s2.intern());
&&System.out.println(s1 == s2.intern());
求最后输出是什么
解答.最后的答案是 false false true
intern()方法.按照jdk的帮助文档来说,是返回字符串对象的规范化表示形式。通俗一点说,就是返回对应这个字符串内容的那个pool里的对象.这样说也许还看不太明白,那可以拿具体例子来说
s1.intern().他的执行流程是,在pool里去查找s1对应的内容(也就是&abc&).如果找到,则返回pool里的对象.如果没有(老实说,我没想到有哪种情况是没有的),则在Pool创建这个对象,并返回...
这样就很容易理解了.s1.intern返回的是pool里的&abc&对象.与s这个堆里的对象肯定不同,返回false.同理,s与s2.intern()也肯定不同,返回false.第三个,s1与s2.intern().其中s2.intern()返回的是pool中的&abc&对象,而s1也是指向pool中的&abc&对象.所以返回的是true:
3第三个问题
String hello = &hello&;
&&String hel = &hel&;
&&String lo = &lo&;
&&System.out.println(hello == &hel& + &lo&);
&&System.out.println(hello == &hel& + lo);
求输出的结果
解答 true false
首先,上面已经说明了,hello hel lo这三个都是指向pool中的对象..
现在我们考虑&hel& + &lo& 按照内容来说,两个相加也就是&hello&.这个时候,这个会返回pool中的&hello&对象.所以,hello == &hel& + &lo& 返回的是true .
而&hel& + lo 虽然内容也是&hello&,但是它将在堆里面生成一个&hello&对象,并返回这个对象...所以这里的结果是false
总结一下就是,如果加号两边的是字面值(字面值就是直接在&&里写的值,比如上面的&hel&与&lo&),那么将在pool里查找有没对应内容的对象(这里的内容就是&hello&),并返回pool里的对象.这和hello是一样的....
如果加号两边不满足上面的条件(比如,两边的值是引用值或者堆里的字符串对象).那么将不会再pool里查找&hello&,而是直接在堆里生成一个新的对象...
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:530465次
积分:4614
积分:4614
排名:第4139名
原创:79篇
评论:167条
(1)(1)(2)(1)(6)(9)(21)(24)(4)(10)(2)(4)

我要回帖

更多关于 display ip pool name 的文章

 

随机推荐