请别再拿“String s = new String(xyz);创建了多少个String实例

引用

问题:

Java代码

Strings=newString("xyz");

创建了几个String Object?

这个问题自身就没有合理的答案,引用的“标准答案”自然也就不准确了:

引用

答案:两个(一个是“xyz”,一个是指向“xyz”的引用对象s)

(好吧这个答案的吐槽点很多……大家慢慢来)这问题的毛病是什么呢?它并没有定义“创建了”的意义。什么叫“创建了”?什么时候创建了什么?而且这段Java代码片段实际运行的时候真的会“创建两个String实例”么?如果这道是面试题,那么可以当面让面试官澄清“创建了”的定义,然后再对应的回答。这种时候面试官多半会让被面试者自己解释,那就好办了,好好晒给面试官看。如果是笔试题就没有提问要求澄清的机会了。不过会出这种题目的地方水平多半也不怎么样。说不定出题的人就是从各种宝典上把题抄来的,那就按照宝典把那不大对劲的答案写上去就能混过去了===============================================================先换成另一个问题来问:

引用

问题:

Java代码

Strings=newString("xyz");

在运行时涉及几个String实例?

一种合理的解答是:

引用

答案:两个,一个是字符串字面量"xyz"所对应的、驻留(intern)在一个全局共享的字符串常量池中的实例,另一个是通过new String(String)创建并初始化的、内容与"xyz"相同的实例

这是根据Java语言规范相关规定可以给出的合理答案。考虑到Java语言规范中明确指出了:

The Java Language Specification, Third Edition 写道

The Java programming language is normally compiled to the bytecoded instruction set and binary format defined inThe Java Virtual Machine Specification, Second Edition(Addison-Wesley, 1999).

也就是规定了Java语言一般是编译为Java虚拟机规范所定义的Class文件,但并没有规定“一定”(must),留有不使用JVM来实现Java语言的余地。考虑上Java虚拟机规范,确实在这段代码里涉及的常量种类为的字符串常量也只有"xyz"一个。CONSTANT_String_info是用来表示Java语言中String类型的常量表达式的值(包括字符串字面量)用的常量种类,只在这个层面上考虑的话,这个解答也没问题。所以这种解答可以认为是合理的。值得注意的是问题中“在运行时”既包括类加载阶段,也包括这个代码片段自身执行的时候。下文会再讨论这个细节与给出的问题的关系。碰到这种问题首先应该想到去查阅相关的规范,这里具体是Java语言规范与Java虚拟机规范,以及一些相关API的JavaDoc。很多人喜欢把“按道理说”当作口头禅,规范就是用来定义各种“道理”的——“为什么XXX是YYY的意思?”“因为规范里是这样定义的!”——无敌了。在Java虚拟机规范中相关的定义有下面一些:

The Java Virtual Machine Specification, Second Edition 写道

2.3 LiteralsA literal is the source code representation of a value of a primitive type, the String type, or the null type.String literals and, more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.The null type has one value, the null reference, denoted by the literal null. The boolean type has two values, denoted by the literals true and false.2.4.8 The Class StringInstances of class String represent sequences of Unicode characters. A String object has a constant, unchanging value.String literalsare references to instances of class String.2.17.6 Creation of New Class InstancesA new class instance is explicitly created when one of the following situations occurs:Evaluation of a class instance creation expression creates a new instance of the class whose name appears in the expression.Invocation of the newInstance method of class Class creates a new instance of the class represented by the Class object for which the method was invoked.A new class instance may be implicitly created in the following situations:Loading of a class or interface that contains a String literal may create a new String objectto represent that literal. This may not occur if the a String object has already been created to represent a previous occurrence of that literal, or if the String.intern method has been invoked on a String object representing the same string as the literal.Execution of a string concatenation operator that is not part of a constant expression sometimes creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type.Each of these situations identifies a particular constructor to be called with specified arguments (possibly none) as part of the class instance creation process.5.1 The Runtime Constant Pool…● A string literalis derived from a CONSTANT_String_info structurein the binary representation of a class or interface. The CONSTANT_String_info structure gives the sequence of Unicode characters constituting the string literal.● The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus,

流过泪的眼睛更明亮,滴过血的心灵更坚强!

请别再拿“String s = new String(xyz);创建了多少个String实例

相关文章:

你感兴趣的文章:

标签云: