小编Has*_* K.的帖子

Java字符串池和类型转换

我的问题是关于Java处理字符串文字的方式.从Java语言规范(JLS)可以清楚地看出,字符串文字是隐式实现的 - 换句话说,就是在堆的String常量池部分中创建的对象,与调用时创建的基于堆的对象形成对比new String("whatever").

似乎与JLS所说的不一致的是,当使用字符串连接创建一个新的String时,使用一个转换的常量String类型,根据JLS应该将其视为一个常量String,显然JVM正在创建一个新的String对象而不是隐式实现它.我感谢有关此特定行为的任何解释以及这是否是特定于平台的行为.我在Mac OSX Snow Leopard上运行.

public class Test
{
    public static void main(String args[])
    {
        /*
            Create a String object on the String constant pool
            using a String literal
        */
        String hello = "hello";
        final String lo = "lo"; // this will be created in the String pool as well
        /*
            Compare the hello variable to a String constant expression
            , that should cause the JVM to implicitly call String.intern()
        */
        System.out.println(hello == ("hel" + …
Run Code Online (Sandbox Code Playgroud)

java compiler-construction string jvm

10
推荐指数
1
解决办法
475
查看次数

标签 统计

compiler-construction ×1

java ×1

jvm ×1

string ×1