设string对象s="hello ",运行语句system.out.println(s.concat("world!"));后string对象s的内容为"hello world!",所以语句输出为hello world!。
时间: 2023-04-20 11:04:15 浏览: 402
回复:是的,运行语句system.out.println(s.concat("world!"));后,string对象s的内容会变成"hello world!",因此输出结果为hello world!。
相关问题
String s0 = "" + sr; System.out.println(s0);
This code will result in a compilation error because "sr" is not a valid variable name and it is not being assigned any value. The correct code should be:
String s0 = "";
System.out.println(s0);
This will create an empty string variable "s0" and print it to the console, which will output a blank line.
public class Test { 2. public static void main(Stringl args){ 3 string s1="ab12" 4. string s2="ab"+"12" 5. String s3="ab12"; 6. String s4=new String("ab")+"12", 7 s3=s3+"c"; 8. System.out.println(s1==s2) 9. system.out println(s2==s3) 10. System.out.println(s1==s4) 9. system.out println(s3==s4)
这段代码有几个错误,需要进行修正:
1. 第二行的main方法参数应该是`String[] args`而不是`Stringl args`。
2. 第三行和第四行的字符串定义应该都加上双引号,即应该是`String s1 = "ab12"`和`String s2 = "ab" + "12"`。
3. 第五行的分号应该是英文的分号`;`而不是中文分号;。
4. 第六行的字符串定义应该是`String s4 = new String("ab") + "12"`。
5. 第七行的字符串拼接中缺少了双引号,应该是`s3 = s3 + "c"`。
6. 第八行和第九行的`System.out.println`应该是小写的`s`而不是大写的`S`。
7. 第十行应该是`System.out.println(s1 == s4)`。
修正后的代码如下所示:
```java
public class Test {
public static void main(String[] args) {
String s1 = "ab12";
String s2 = "ab" + "12";
String s3 = "ab12";
String s4 = new String("ab") + "12";
s3 = s3 + "c";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1 == s4);
System.out.println(s3 == s4);
}
}
```
这段代码的输出结果是:
```
true
false
false
false
```
其中第一个输出结果为true,是因为s1和s2都是通过字符串常量"ab12"生成的,它们在内存中的地址相同。
第二个输出结果为false,是因为s2和s3虽然都是"ab12",但是s2是通过字符串常量拼接得到的,而s3是直接定义的,所以它们在内存中的地址不同。
第三个输出结果为false,是因为s1和s4虽然都是"ab12",但是s4是通过new String()方法生成的,它在内存中的地址与s1不同。
第四个输出结果为false,是因为s3和s4都经过了字符串拼接,它们在内存中的地址也不同。