小编Ada*_*dam的帖子

Java字符串创建和字符串常量池

使用关键字new创建String时,它使用带有String文字的构造函数创建一个新的String对象.我想知道在调用String构造函数之前文字是否存储在常量池中.

我问的原因是,在"OCA Java SE 7程序员I认证指南"中,Mala Gupta写道:

public static void main(String[] args)
{
    String summer  = new String("Summer");   //Line 1: The code creates a new String object with the value "Summer". This object is not placed in the String constant pool.
    String summer2 = "Summer"                //Line 2: The code creates a new String object with the value "Summer" and places it in the String constant pool.
}
Run Code Online (Sandbox Code Playgroud)

她在第一行说,new创建的String对象没有存储在常量池中.这很好,但不清楚的是,如果第一行的构造函数中的文字"Summer"是.

在第二行,她说"Summer"的分配到summer2将它存储在常量池中,这意味着第一行的文字没有放在池中.

我的问题

  1. 第1行:在调用String构造函数之前,构造函数中的文字"Summer"是否放在常量池中?
  2. 第2行:第2 行 …

java string string-literals

12
推荐指数
1
解决办法
1316
查看次数

Java字符串创建和字符串池

使用关键字new创建String时,它使用带有String文字的构造函数创建一个新的String对象.

在调用String构造函数之前,文字是否存储在常量池中?

String hello = new String("Hello");
String literal = "Hello";             //Does "Hello" already exist in the pool
                                      //or is it inserted at this line?
Run Code Online (Sandbox Code Playgroud)

编辑

在"OCA Java SE 7程序员I认证指南"中,Mala Gupta写道:

public static void main(String[] args)
{
    String summer  = new String("Summer");   // The code creates a new String object with the value "Summer". This object is not placed in the String constant pool.
    String summer2 = "Summer"                // The code creates a new String object with the …
Run Code Online (Sandbox Code Playgroud)

java string

2
推荐指数
1
解决办法
128
查看次数

标签 统计

java ×2

string ×2

string-literals ×1