Java String pool and type casting

前端 未结 2 965
暗喜
暗喜 2021-02-14 07:21

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 ot

2条回答
  •  [愿得一人]
    2021-02-14 07:59

    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:

    > - Casts to primitive types and casts to type String

    (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.)

提交回复
热议问题