Java字符串池和类型转换 [英] Java String pool and type casting

查看:173
本文介绍了Java字符串池和类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于Java处理字符串文字的方式。从Java语言规范(JLS)中可以清楚地看出,字符串文字被隐式地嵌入了 - 换句话说,在堆的String常量池部分创建的对象,与调用<$时创建的基于堆的对象c $ c> new String(whatever)。

My question is in regard to the way Java handles String literals. It's quite clear from the Java Language Specs (JLS) that String literals are being implicitly interned - in other words, objects that are created in the String constant pool part of the heap, in contrast to the heap-based objects created when calling new String("whatever").

什么似乎不符合JLS说的是,创建一个新的字符串使用字符串连接与一个强制的常量String类型,这应该被视为一个常量字符串按照JLS,显然JVM正在创建一个新的String对象,而不是隐式隐藏它。我喜欢关于这个特定行为的任何解释,以及这是否是一个平台特定的行为。我在Mac OSX雪豹运行。

What doesn't seem to line up with what the JLS says is that when creating a new String using String concatenation with a casted constant String type, which should be considered as a constant String as per the JLS, apparently the JVM is creating a new String object rather than interning it implicitly. I appreciate any explanation about this particular behaviour and whether or not this is a platform-specific behaviour. I am running on a 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" + lo));// This should print true
        /*
            Here we need to create a String by casting an Object back
            into a String, this will be used later to create a constant
            expression to be compared with the hello variable
        */
        Object object = "lo";
        final String stringObject = (String) object;// as per the JLS, casted String types can be used to form constant expressions
        /*
            Compare with the hello variable
        */
        System.out.println(hello == "hel" + stringObject);// This should print true, but it doesn't :(

    }
}


推荐答案

投放到对象在编译时常量表达式中是不允许的,只有 String 和原语JLS(Java SE 7 edition)section 15.28:

Casting to Object is not allowed in a compile time constant expression. The only casts permitted are to String and primitives. JLS (Java SE 7 edition) section 15.28:

> - 转换为原始类型并转换为类型String

(实际上有第二个原因。 object isn 't final 所以不能考虑a 常量变量。原始类型或类型的变量 String ,即 final 并使用编译时常量表达式(§15.28)初始化,称为常量变量。 - 第4.12.4节)

(There's actually a second reason. object isn't final so cannot possibly by considered a constant variable. "A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable." -- section 4.12.4.)

这篇关于Java字符串池和类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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