Java如何处理内存中的String对象? [英] How does Java handle String objects in memory?

查看:123
本文介绍了Java如何处理内存中的String对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被问到这个问题:

String s = "abc"; // creates one String object and one
          // reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects,
                 // and one reference variable*

基于以上详细信息在下面的代码的println语句之前创建了多少个String对象和多少个引用变量?

Based on above details how many String objects and how many reference variables were created prior to the println statement of below code?

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

我的回答是
此代码片段的结果是春夏春夏

My answer was The result of this code fragment is spring winter spring summer

有两个参考变量,s1和s2。总共有八个String对象
创建如下:spring,summer(丢失),spring summer,fall(丢失),spring
fall(丢失) ,春夏(失落),冬天(迷失),春天的冬天(此时春天失去了)。

There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: "spring", "summer " (lost), "spring summer", "fall" (lost), "spring fall" (lost), "spring summer spring" (lost), "winter" (lost), "spring winter" (at this point "spring" is lost).

在此过程中,八个String对象中只有两个没有丢失。

Only two of the eight String objects are not lost in this process.

是否正确?

推荐答案

答案是:2个引用和8个对象。

Answer is: 2 references and 8 objects.

String s1 = "spring "; //One reference and 1 object in string pool. (if it didn't exist already)

String s2 = s1 + "summer "; //Two references and 3 objects

s1.concat("fall "); //Two references and 5 objects

s2.concat(s1); //Two references and 6 objects

s1 += "winter "; //Two references and 8 objects

System.out.println(s1 + " " + s2);

现在你的问题: Java如何处理内存中的String对象?

Java提供了两种创建类 String 对象的方法。

Java provides two ways to create object of class String.


  1. 字符串str1 =OneString;

在这种情况下,JVM搜索字符串池看看是否已存在等效字符串。如果是,则返回对该引用的引用。如果没有,将其添加到字符串池并返回引用。 因此可能会创建一个新对象,也可能不会。

In this case, JVM searches the string pool to see if equivalent string exist already. if yes, returns the reference to same. if not, adds it to string pool and returns the reference. So a new object may be created OR may not be.


  1. String str1 = new String(OneString);

现在,JVM必须在堆上创建一个对象。由于。如果 OneString 已存在于字符串池中,则无关紧要。

Now, JVM has to create an object on heap. due to new. it doesn't matter if OneString is already present in string pool.

您还可以将字符串放入池中:

You can also put a string to pool:


您可以在String对象上调用intern()。这将把String对象放在池中(如果它尚不存在),并返回对池化字符串的引用。 (如果它已经在池中,它只返回对已存在的对象的引用)。

You can call intern() on a String object. This will put the String object in the pool if it is not already there, and return the reference to the pooled string. (If it was already in the pool, it just returns a reference to the object that was already there).

您可以查看以下链接:

什么是Java字符串池以及s是什么?与新字符串(" s")不同?

关于Java字符串池的问题

这篇关于Java如何处理内存中的String对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆