问题描述
我知道问题的标题不是很清楚,对不起,不知道如何提出。我有一个非常基本的java实现问题,我想关注应用程序性能,但它也涉及java中的String创建模式。
I know that the title of the question is not very clear, sorry about that, did not know how to put it up. I have a very basic java implementation question which I want to focus on application performance, but it also involves String creation pattern in java.
我理解字符串的不变性概念Java的。我不确定的是,我在某处读到以下内容不会产生两个不同的String对象:
I understand the immutability concept of Strings in Java. What I am not sure about is that, I have read somewhere that the following will not make two different String objects:
String name = "Sambhav";
String myName= "Sambhav";
我想知道Java是如何做到的?它是否实际在程序存储器中查找String值并检查它是否存在,如果它不存在则创建一个新的String对象?
在这种情况下,显然它会节省内存,但存在性能问题。
I want to know how does Java do that? Does it actually look for a String value in the program memory and check for its existence and if it does not exist then creates a new String object?In that case obviously it is saving memory but there are performance issues.
还可以说我有这样的代码:
Also lets say I have a code like this:
public void some_method(){
String name = "Sambhav";
System.out.println(name); // or any random stufff
}
现在每次调用此函数时,都有一个新的String正在制作并添加到内存中或我使用相同的String对象?
我只是很想知道这一切是如何发生的见解?
Now on each call of this function, is there a new String being made and added to memory or am I using the same String object?I am just curious to know about the insights of how all this is happening?
此外,如果我们说
String name = "Sambhav";
String myName= "Sambhav";
因为参考不会创建新对象,那么
will not create a new object because of reference, what about
String name = new String("Sambhav");
String myName= new String("Sambhav");
Java仍能捕获字符串是否相同,只是将myName指向同一个对象在前一个语句中创建的?
Will Java still be able to catch that the string are the same and just point myName to the same object as created in the previous statement?
推荐答案
字符串是内部char数组,具有一些固有的功能来处理底层char数组。例如。 subString(int),split(String)方法。
Strings are internally char arrays with some inherent capabilities to work with the underlying char array. Eg. subString(int), split(String) methods.
字符串是不可变的,这意味着为更改String引用所做的任何努力都会创建一个新的String并为其分配内存。如下所示
Strings are immutable which means any effort made to change a String reference create a new String and allocate memory for that. As below
line 1. String a = new String("SomeString");
line 2. a = "SomeStringChanged";
第1行用变量a引用的SomeString分配内存并添加 SomeString to 字符串池
line 1 allocate memory with "SomeString" referenced by variable a and add "SomeString" to String Pool
第2行使用SomeStringChanged在字符串池中分配内存并引用。即a现在没有指向SomeString,SomeString占用的内存现在可用于 gc 。
line 2 allocate memory in String Pool with "SomeStringChanged" and referenced by a. i.e a is not pointing to "SomeString" now and memory occupied by "SomeString" is available for gc now.
此处不再重用
line 3. String b = "SomeStringChanged";
现在文字 SomeStringChanged 重复使用按变量 a 和 b 。即他们指的是相同的记忆位置,实际上是指一个名为' String Pool '的位置。
Now the literal "SomeStringChanged" is reused by variable a and b. i.e they are referring to the same memory location, in fact to a location called the 'String Pool'.
line 4. a = new String("SomeStringChanged");
现在完成了一个新的分配,其中包含 SomeStringChanged 并由 a
Now a new allocation is done which contains "SomeStringChanged" and referenced by a
现在有无法重复使用。 (char数组 SomeStringChanged 已存在于String Pool中。因此不会发生字符串池分配)
There is no reuse happening now. (the char array SomeStringChanged is already there in String Pool. So no String Pool allocation happen)
line 5. a = new String("SomeStringChanged").intern();
现在,第4行创建的分配将被丢弃,变量 a 和 b 指的是字符串池中包含SomeStringChanged的相同位置。这里有重复使用相同的char数组。功劳归于实习生()方法
Now the allocation created during line 4 is discarded and variable a and b are referring to same location in the String Pool which contains "SomeStringChanged". There is reuse of the same char array here. The credit goes to intern() method
line 6. String x = new String("SomeX");
line 7. String y = "SomeX";
第6行将在堆和字符串中为 SomeX 创建分配池。 char数组是重复的。
Line 6 will create an allocation for SomeX in the heap and in String Pool. The char array is duplicated.
第7行不会为 SomeX 分配任何内存,因为它已存在于String Pool中
Line 7 will not allocate any memory for SomeX since its already there in the String Pool
Line 8 String s = new String(someStringVariable);
第8行只会在堆中分配单个内存位置,而不会在字符串池中分配。
Line 8 will only allocate single memory location in the heap and not in the String Pool.
总之,只有将String引用声明为文字或String对象被实现时才可以重用字符串的char数组,即只有这两个才能使用字符串pool(实际上是char数组重用背后的想法)。
In conclusion the reuse of a char array of string is only possible if a String reference is declared as a literal or the String object is interned i.e Only these two can make use of a String pool (which is in fact the idea behind char array reuse).
这篇关于Java String Immutability和使用相同的字符串值来创建新字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!