When do (not) two strings with same content share the same memory?

我们两清 提交于 2019-12-02 01:07:05

问题


Coming from the question will two strings with same content be stored in the same memory location?

Having the Java code

String s1="Java";

will this string be allocated in the same memory location (or multipe):

  • if to launch the same program multiple times executing it in parallel (concurrently)?

Possible answer:

I am currently C# developer (though programmed in Java in the previous millennium).

I asked this question because I believed it is the same between .NET CLR and Java (JVM) and I was hoping to get the answer for .NET apps (but somehow was in doubt by frequently encountered "application" pool terms).

So, it seems to be (sorry for not exhaustively searching before asking):

  • Does the CLR/JVM keep one single intern pool for all running .net/java apps?

with the answer that string intern pool is shared per all instances/programs of the same JVM or .NET CLR.


回答1:


@gennady-vanin-novosibirsk....

String s1="Java";String s2="Java";String s3="Java";String s4="Java";

  1. The above all objects available in StringConstantPool location those are all objects are pointing to only one ("Java") location



回答2:


Is string pool created per class basis or per JVM basis?

There is one String pool per JVM ... unless you are using some exotic JVM where they've decided to implement it differently.

I think that answers it, right?

from similar question




回答3:


Same memory location:

String s1="Hola";
String s2="Hola";

Distinct memory location:

String s1="Hola";
String s2=new String("Hola"); 



回答4:


Strings of same contents will share same space for every instance of same JVM



来源:https://stackoverflow.com/questions/15827344/when-do-not-two-strings-with-same-content-share-the-same-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!