.以下程序的输出结果是? public class Example { String str = new string("good""); char[ch= i 'a", "b, "c' 3; public static void main(String args[]){ Example ex =new Example(); ex.change(ex.str, ex.ch); System.out.print(ex.str + " and ");system.out.print(ex.ch); } public static void change( String str, char ch[]i str = "test ok";ch[e] = 'g ”; ) } A good and gbc B test ok and abc C test ok and gbc D good and abc
时间: 2024-01-14 10:02:31 浏览: 39
该程序存在语法错误,无法通过编译。具体来说,第 2 行字符串常量的末尾多了一个多余的双引号,应该改为 `"good"`;第 3 行字符数组的初始化方式不正确,应该改为 `char[] ch = {'a', 'b', 'c'};`。在进行修正后,程序输出的结果应该是 C 选项 `test ok and gbc`。原因是,在 change 方法中,虽然修改了传入的字符串和字符数组,但是这些修改只在方法内部有效,并不会影响到 main 方法中的变量值。因此,main 方法中的 ex.str 仍然是原来的 "good",而 ex.ch[1] 被修改成了 'g'。
相关问题
public class Example{ String str=new String("good"); char[]ch={'a','b','c'}; public static void main(String args[]){ Example ex=new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str+" and "); Sytem.out.print(ex.ch); } public void change(String str,char ch[]){ str="test ok"; ch[0]='g'; } }
您提供的代码存在以下问题:
1. 第10行的 `Sytem.out.print` 应该为 `System.out.print`,即拼写错误。
2. 第16行的 `str="test ok"` 和第17行的 `ch[0]='g'`,虽然在方法内部执行了,但是并没有对 `Example` 类的成员变量 `str` 和 `ch` 进行修改。这是因为 Java 采用的是按值传递,即传递的是对象的引用值,而不是对象本身。因此,在方法内部修改参数的值,并不会影响到原对象的值。
以下是修改后的代码:
```
public class Example {
String str = new String("good");
char[] ch = {'a', 'b', 'c'};
public static void main(String args[]) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
}
public void change(String str, char ch[]) {
str = "test ok";
ch[0] = 'g';
}
}
```
输出结果为:
```
good and gbc
```
可以看到,虽然在方法内部修改了参数的值,但是并没有影响到原对象的值。
指出下列程序的运行结果: public class Example{ String str = new String(“good”); char[] ch = {‘a’,’b’,’c’}; public static void main(String[] args){ Example ex= new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str += ” and ”); System.out.print(ex.ch); } public void change(String str,char ch[]){ str = “test ok”; ch[0] = ‘g’; } } A. good and abc B. good and gbc C. test ok and abc D. test ok and gbc
选项 B. good and gbc 是该程序的运行结果。
在该程序中,创建了一个 Example 类的对象 ex,并调用了 change 方法。在 change 方法中,虽然修改了 ch 数组中的第一个元素,但是修改 str 的语句只是将 str 参数指向了一个新的字符串对象,并不会改变 ex.str 的值。
因此,在主方法中,ex.str 的值仍然是 "good",但是 ex.ch 数组的第一个元素已经被改变为 'g',因此输出的结果为 "good and gbc"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)