简体   繁体   English

在编译时或运行时生成 Java 字符串

[英]Java String generation at compile time or runtime

Given this code:鉴于此代码:

if(somecondition) {
    String one = "one";
    //print one;
}

This string will only be generated when that condition is true?只有在该条件为真时才会生成此字符串?

Appreciate any help.感谢任何帮助。

Edit:编辑:

With String pooling, is it safe to say that String one will be added to the pool regardless of a condition.使用字符串池,可以肯定地说,无论条件如何,字符串one都会被添加到池中。

So, if a variable needs to be resolved from an object, what will happen?那么,如果需要从对象解析变量,会发生什么?

Say,说,

String hello = "Hello Mr " + user.firstName();

How will this be added to String pool?这将如何添加到字符串池中? And when it does get added to String pool, it will not create new String literals right (unless user.firstName() changes).并且当它确实被添加到字符串池时,它不会正确地创建新的字符串文字(除非 user.firstName() 更改)。

The string is generated at the time you typed it;该字符串在您键入时生成; but, for that answer to make sense, we need to walk it's life through the transformations in the build and launch of the application.但是,为了让这个答案有意义,我们需要在应用程序的构建和启动过程中经历转换。

  1. First you typed it into the *.java file.首先,您将其输入到*.java文件中。
  2. Then the compiler copied it into the *.class file as part of the constant pool entries.然后编译器将其作为常量池条目的一部分复制到*.class文件中。
  3. Then the program was run, and the class was requested, which triggered the class loader to copy the file from disk to RAM, triggering a object meta-data entry being created to wrap the constant in the constant pool.然后运行程序并请求类,这触发类加载器将文件从磁盘复制到 RAM,触发创建的对象元数据条目以将常量包装在常量池中。

So, there's multiple places where it could be "created" depending on your definition of which kind of "creation" you wish to use.因此,根据您对希望使用的“创建”类型的定义,可以在多个位置“创建”它。

Now, in your first example, the String object isn't realized when you use the string, but when you use the *.class file.现在,在您的第一个示例中,使用字符串时并未实现 String 对象,而是使用*.class文件时。 However, it's not reference by the running program until you enter the method.但是,在您输入方法之前,它不会被正在运行的程序引用。

Finally, with String pooling, every time a string is about to be created, the existing pool of strings is searched, and if a matching entry is found, the matching entry is used instead of creating a new string.最后,使用字符串池化,每次将要创建字符串时,都会搜索现有字符串池,如果找到匹配条目,则使用匹配条目而不是创建新字符串。 This reduces the number of strings in a runtime, at the cost of a lot of string searching.这以大量字符串搜索为代价减少了运行时中的字符串数量。

Due to the details of your code, you have three different strings that are eligible for pooling ("Hello Mr ", the value of user.firstName() , and the string combining them both).由于您的代码的详细信息,您有三个不同的字符串符合池化条件(“Hello Mr”, user.firstName()的值,以及将它们组合在一起的字符串)。 "Hello Mr " would be pooled with the class loading (assuming pooling is being done). “你好先生”将与类加载合并(假设正在完成合并)。 The value of user.firstName() would have happened when the value for the return was originally created. user.firstName()的值会在最初创建返回值时发生。 The resulting combined string would be pooled just before the assignment (or reference from the pool, if it already exists in the pool).生成的组合字符串将在分配之前(或来自池的引用,如果它已经存在于池中)被池化。

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

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