1. 阅读以下程序,写出程序运行结果。 public class Test { public static void main(String[] args) { String str1 = "百年追梦"; String str2 = "百年追梦"; String str3 = new String("百年追梦"); String str4 = new String("百年追梦"); String str5 = "胸怀千秋伟业,恰是百年风华!"; System.out.println(str1 == str2); System.out.println(str3 == str4); System.out.println(str3.equals(str4)); System.out.println(str5.length()); System.out.println(str5.substring(9,13)); System.out.println(str5.indexOf("伟业")); } }
时间: 2024-01-18 22:04:06 浏览: 24
程序运行结果如下:
```
true
false
true
13
伟业
6
```
解释如下:
1. `str1`和`str2`都是直接赋值的字符串常量,它们的值相同,都指向字符串常量池中的同一个对象,因此`str1 == str2`的结果为`true`。
2. `str3`和`str4`都是通过`new`关键字创建的字符串对象,它们的值相同,但是它们指向堆内存中的不同对象,因此`str3 == str4`的结果为`false`。
3. `str3.equals(str4)`比较的是字符串对象的值是否相同,因此结果为`true`。
4. `str5.length()`返回字符串长度,结果为`13`。
5. `str5.substring(9,13)`返回字符串中索引为9到12的子串,结果为`伟业`。
6. `str5.indexOf("伟业")`返回第一次出现`"伟业"`的位置,结果为`6`。
相关问题
给定以下代码,程序的运行结果是 () public class Test { public static void main(String[] args) { String result = reverse("happy"); System.out.println(result); } public static String reverse(String str) { StringBuilder result = new StringBuilder(""); char[] chArra = str.toCharArray(); for (int i = chArra.length - 1; i >= 0; i--) { char ch = chArra[i]; result.append(ch); } return result.toString(); } }
程序的运行结果是 `yppah`。
原因是:程序定义了一个静态方法`reverse`,该方法接受一个字符串参数`str`,用StringBuilder对象`result`记录反转后的结果。程序首先将字符串`str`转换为字符数组`chArra`,然后从后往前遍历该数组,将每个字符追加到`result`对象中。最后将`result`对象转换为字符串并返回。
在main方法中,程序调用`reverse`方法并将字符串`"happy"`作为参数传入,`reverse`方法返回的结果为`"yppah"`,程序将该结果打印到控制台。
public class Test { public static void main(String args[]) { String Str = new String("菜鸟教程:www.runoob.com"); boolean retVal; retVal = Str.endsWith( "runoob" ); System.out.println("返回值 = " + retVal ); retVal = Str.endsWith( "com" ); System.out.println("返回值 = " + retVal ); } }
这段代码是一个简单的Java程序。它创建了一个名为 `Test` 的公共类,并包含一个 `main` 方法作为程序的入口点。
在 `main` 方法中,首先创建了一个名为 `Str` 的字符串对象,其值为 "菜鸟教程:www.runoob.com"。
接下来,声明了一个布尔类型的变量 `retVal`,用于存储方法 `endsWith()` 的返回值。
然后,通过调用 `Str.endsWith("runoob")` 来判断字符串 `Str` 是否以 "runoob" 结尾,并将结果赋值给 `retVal` 变量。
接着,使用 `System.out.println()` 方法将 `"返回值 = "` 和 `retVal` 的值打印输出。
然后,再次调用 `Str.endsWith("com")` 来判断字符串 `Str` 是否以 "com" 结尾,并将结果赋值给 `retVal` 变量。
最后,再次使用 `System.out.println()` 方法将 `"返回值 = "` 和 `retVal` 的值打印输出。
运行该程序将会输出如下结果:
```
返回值 = false
返回值 = true
```
这是因为字符串 "菜鸟教程:www.runoob.com" 不以 "runoob" 结尾,但以 "com" 结尾。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)