下列语句输出的结果是( )。 String s1="abcd"; String s2="acd"; System.out.println(s1.compareTo(s2)); A. 1 B. -1 C. 98 D. -98
时间: 2024-04-02 07:31:45 浏览: 119
答案是B. -1。
String类的compareTo()方法比较两个字符串,如果第一个字符串小于第二个字符串,则返回一个小于0的值;如果第一个字符串大于第二个字符串,则返回一个大于0的值;如果两个字符串相等,则返回0。在这个例子中,s1中的'b'的Unicode码(98)大于s2中的'd'的Unicode码(100),因此s1大于s2,返回一个小于0的值,即-1。
相关问题
写出代码运行的结果 String s="asgfasg"; String s3="asgfasg"; String s1=new String(s); String s2="SDFSdfs"; System.out.println(s.charAt(4)); //结果1 System.out.println(s.compareTo(s1)); //结果2 System.out.println(s.compareTo(s2)); //结果3 System.out.println(s.concat(s2)); //结果4 System.out.println(s); //结果5 System.out.println(s2.contains("dfs")); //结果6 System.out.println(s.equals(s1)); //结果7 System.out.println(s.substring(2,5)); //结果8 System.out.println(s==s3); //结果9 System.out.println(s==s1); //结果10 System.out.println(String.valueOf(53.56)); //结果11
代码运行的结果如下:
```
g
0
-19
asgfasgSDFSdfs
asgfasg
true
true
gfa
true
false
53.56
```
解释如下:
1. 输出字符串s中索引为4的字符,即 'g'。
2. 比较字符串s和s1,两个字符串相等,返回0。
3. 比较字符串s和s2,s2的字符序列在字典序上比s小,返回一个负数。
4. 将字符串s和字符串s2连接起来,返回一个新的字符串 "asgfasgSDFSdfs"。
5. 输出字符串s的内容,即 "asgfasg"。
6. 判断字符串s2中是否包含子字符串 "dfs",返回true。
7. 比较字符串s和s1,两个字符串相等,返回true。
8. 返回字符串s中索引从2到5的子串,不包括索引5,即 "gfa"。
9. 字符串s和字符串s3引用同一个字符串对象,返回true。
10. 字符串s和字符串s1引用不同的字符串对象,返回false。
11. 将浮点数53.56转化为字符串 "53.56"。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)