正解!更具体的要看<<Java语言规范>>章节<15.28 Constant Expression>(常量表达式)15.28 Constant Expression ConstantExpression: ExpressionA compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: * Literals of primitive type and literals of type String (§3.10.5) * Casts to primitive types and casts to type String * The unary operators +, -, ~, and ! (but not ++ or --) * The multiplicative operators *, /, and % * The additive operators + and - * The shift operators <<, >>, and >>> * The relational operators <, <=, >, and >= (but not instanceof) * The equality operators == and != * The bitwise and logical operators &, ^, and | * The conditional-and operator && and the conditional-or operator || * The ternary conditional operator ? : * Parenthesized expressions whose contained expression is a constant expression. * Simple names that refer to constant variables (§4.12.4). * Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4). Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2). Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.A compile-time constant expression is always treated as FP-strict (§15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.Examples of constant expressions: true (short)(1*2*3*4*5*6) Integer.MAX_VALUE / 2 2.0 * Math.PI "The integer " + Long.MAX_VALUE + " is mighty big."
2楼是正确的。 测试一下,首先建立一个源文件名为StringTest.java,内容如下: public class StringTest { public static void main(String[] args) { String s3 = "sss" + "111"; } } ############################################################# javap -c StringTest : 以下JVM的产生的字节码: ################################################################ public class StringTest extends java.lang.Object{ public StringTest(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: returnpublic static void main(java.lang.String[]); Code: 0: ldc #2; //String sss111 2: astore_1 3: return}从“#2; //String sss111”这条语句可以看出,只创建了一个字符串。
由于常量的值在编译的时候就被确定了。在这里,"sss"和"111"都是常量,因此变量s3 的值在
编译时就可以确定。这行代码编译后的效果等同于:
String s3 ="sss111";
因此这里只创建了一个对象"sss111",并且它被保存在字符串池里了。
String str1 = "sss";
String str2 = "111";
String str3 = str1 + str2;
这样是3个。
支持下二楼的。
应该是一个
通过连接操作得到的String(非常量表达式),连接操作是运行时进行的,会新创建对象,所以它们是不同的。
楼主的例题是常量表达式 所以只有一个
如果是 String s2="ss";
String s3 = s2 + "111";
则会产生两个 可以看下zangxt 的总结
http://topic.csdn.net/u/20090519/18/7b8cf7ef-bc06-4d26-8a2c-692eb0562231.html
正解!更具体的要看<<Java语言规范>>章节<15.28 Constant Expression>(常量表达式)15.28 Constant Expression
ConstantExpression:
ExpressionA compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: * Literals of primitive type and literals of type String (§3.10.5)
* Casts to primitive types and casts to type String
* The unary operators +, -, ~, and ! (but not ++ or --)
* The multiplicative operators *, /, and %
* The additive operators + and -
* The shift operators <<, >>, and >>>
* The relational operators <, <=, >, and >= (but not instanceof)
* The equality operators == and !=
* The bitwise and logical operators &, ^, and |
* The conditional-and operator && and the conditional-or operator ||
* The ternary conditional operator ? :
* Parenthesized expressions whose contained expression is a constant expression.
* Simple names that refer to constant variables (§4.12.4).
* Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4). Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2). Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.A compile-time constant expression is always treated as FP-strict (§15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.Examples of constant expressions: true
(short)(1*2*3*4*5*6)
Integer.MAX_VALUE / 2
2.0 * Math.PI
"The integer " + Long.MAX_VALUE + " is mighty big."
测试一下,首先建立一个源文件名为StringTest.java,内容如下:
public class StringTest {
public static void main(String[] args) {
String s3 = "sss" + "111";
}
}
#############################################################
javap -c StringTest
:
以下JVM的产生的字节码:
################################################################
public class StringTest extends java.lang.Object{
public StringTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: returnpublic static void main(java.lang.String[]);
Code:
0: ldc #2; //String sss111
2: astore_1
3: return}从“#2; //String sss111”这条语句可以看出,只创建了一个字符串。
是至少 三个
sss 111 sss111
如果是这样问的话:执行这一句之后,创建了几个String对象
那么答案是一个。