简体   繁体   English

JVM如何处理字符串分配?

[英]How does the JVM handle string assignments?

Given this code: 给出以下代码:

String first = "Hello world";

String second = first;

first = "Something else";

After the execution, will the variable second point to the same memory instance that the variable first pointed in the first assignment (the same "Hello world") or will it be a completely different memory region (another memory region which also says "Hello world") ? 执行后, second变量将指向第一个赋值中变量first指向的相同内存实例(相同的“ Hello world”),或者将是一个完全不同的内存区域(另一个也称为“ Hello world”的内存区域) “)?

I want to know if making multiple assignments like in the second line (String other = originalString) causes any performance loss or if it's as fast as assigning any other object. 我想知道是否像第二行(String other = originalString)那样进行多个分配会导致性能下降,或者是否与分配任何其他对象一样快。

After the execution, will the variable second point to the same memory instance that the variable first pointed in the first assignment (the same "Hello world") or will it be a completely different memory region (another memory region which also says "Hello world") ? 执行后,第二个变量将指向第一个赋值中变量第一个指向的相同内存实例(相同的“ Hello world”),或者是一个完全不同的内存区域(另一个也称为“ Hello world”的内存区域) “)?

The same memory region. 相同的内存区域。

Here's what you have at each stage: 这是每个阶段的内容:

String first = "Hello world";

Gives you: 给你:

+-------+           +---------------+
| first |---------->| "Hello world" |
+-------+           +---------------+

Then 然后

String second = first;
+--------+
| second |----\
+--------+     |     +---------------+
               +---->| "Hello world" | (same memory as above)
+--------+     |     +---------------+
| first  |----/
+--------+

Then 然后

first = "Something else";
+--------+           +---------------+
| second |---------->| "Hello world" | (same memory as above)
+--------+           +---------------+
+--------+           +------------------+
| first  |---------->| "Something else" |
+--------+           +------------------+

Second is a reference variable. 第二是参考变量。 Like any reference variable, assigning it to another causes only the reference to be copied, not the object. 像任何引用变量一样,将其分配给另一个变量只会导致引用被复制,而不是对象。 The two reference variables point to the same memory. 两个参考变量指向相同的存储器。

I want to know if making multiple assignments like in the second line (String other = originalString) causes any performance loss or if it's as fast as assigning any other object. 我想知道是否像第二行(String other = originalString)那样进行多个分配会导致性能下降,或者是否与分配任何其他对象一样快。

You're not assigning an object; 您没有分配对象; you're assigning a reference to an object. 您正在为对象分配引用。 Copying a reference to a string is no more expensive than copying any other reference. 复制对字符串的引用并不比复制任何其他引用贵。 Copying a reference variable is very cheap. 复制参考变量非常便宜。 Usually you can copy the reference values as required for readability and understandability, without significant effect on performance. 通常,您可以根据需要复制参考值,以提高可读性和可理解性,而不会对性能产生重大影响。

String literals are immutable in java. 字符串文字在Java中是不可变的。 Once a String literal is created it is cannot be modified and is stored on a string constant pool. 创建字符串文字后,将无法对其进行修改并将其存储在字符串常量池中。 Here is how it will work in your case: 这是在您的情况下如何工作的方法:

String first = "Hello world";

New string will be created on a constant pool and 'first' will point to it. 新字符串将在常量池上创建,并且“ first”将指向它。

String second = first;

Both the references first and second will point to the same string object. 引用第一个和第二个都将指向同一字符串对象。

first = "Something else";

The String object 'Hello world' will not be modified in this case. 在这种情况下,将不会修改String对象“ Hello world”。 Instead a new object 'Something else' will be created and 'first' will start pointing to it. 而是将创建一个新对象“其他”,并且“第一个”将开始指向该对象。 While the 'second' reference keeps pointing to the object 'Hello world'. 虽然“第二”引用始终指向对象“ Hello world”。

Now lets say you create a new reference something like this : 现在假设您创建了一个新的引用,如下所示:

String third = "Something else";

Reference 'third' will also start pointing to the same object as first, even though you have not assigned the reference value of first to it using '=' operator. 即使您尚未使用“ =”运算符将first的参考值分配给它,引用“ third”也将开始指向与first相同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM