首页 > 试题广场 >

检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说

[问答题]
检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。
package algorithms.com.guan.javajicu;
public class Example {
    String str = new String(“good”);
    char[] ch = {‘a’, ’b’, ’c’};
    public static void main(String[] args) {
        Exampleex = new Example();
        ex.change(ex.str, ex.ch);
        System.out.print(ex.str + ” and ”);
        System.out.print(ex.ch);
 }
 public void change(Stringstr, char ch[]) {
        str = “test ok”;
        ch[0] = 'g';
 }
}

除去格式上的错误,姑且认为是排版有错误吧。结果是:good  and gbc。
java中方法接收的参数都是值传递。
对于这个题目:change方法接收了两个参数,分别是:str和ch,方法体内部又创建出两个局部变量分别等于这两个参数,即是:str‘=str,ch’=ch,此时str‘指向的是堆中的“good”,ch’指向的是堆中的char[],内容是{‘a’,‘b‘,’c‘}。str的类型是string,这种类型在java中是不能修改的,当str’要重新赋值的时候,其实是又实例化了一个新的string,值是“test ok”,原来的str(“good”)是没有变化的;ch‘[0]=‘g’时,也就是修改了ch‘所指向的堆中的ch[],所以内容修改了,变成{‘g’,‘b’,‘c’}。
发表于 2015-08-22 17:55:28 回复(0)
这个程序不是本身就有问题吗?怎么都说没问题呢
发表于 2015-08-03 15:26:19 回复(2)
输出结果为:good and gbc
Java的值传递和引用传递
String类型作为char[]的包装类,在作为方法参数传递时,体现的是非对象的特性,是值传递,不会改变实际参数的值
char[]数组是引用数据类型,是引用传递,当没有改变地址时,能够改变实际参数的值
发表于 2015-07-17 10:28:39 回复(1)
good  and  g b c
发表于 2016-03-15 18:24:09 回复(0)
goodandgbc
发表于 2015-10-08 16:07:33 回复(0)
程序语法错误,编译都不会通过的~
发表于 2015-10-04 23:56:18 回复(0)
<div class="line number1 index0 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> packagealgorithms.com.guan.javajicu; </div> <pre class="prettyprint lang-java"></pre> <div class="line number2 index1 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> publicclassExample { </div> <div class="line number3 index2 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;String str =&nbsp;newString("good"); </div> <div class="line number4 index3 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;char[] ch = {'a','b', 'c'}; </div> <div class="line number5 index4 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;publicstaticvoidmain(String[] args) { </div> <div class="line number6 index5 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Example ex =&nbsp;newExample(); </div> <div class="line number7 index6 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.change(ex.str, ex.ch); </div> <div class="line number8 index7 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(ex.str + " and "); </div> <div class="line number9 index8 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(ex.ch); </div> <div class="line number10 index9 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;} </div> <div class="line number11 index10 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;publicvoidchange(String str,&nbsp;charch[]) { </div> <div class="line number12 index11 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str = "test ok"; </div> <div class="line number13 index12 alt2" style="margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch[0] =&nbsp;'g'; </div> <div class="line number14 index13 alt1" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> &nbsp;} </div> <div class="line number15 index14 alt2" style="color:#333333;margin:0px !important;padding:0px 1em !important;vertical-align:baseline !important;"> } </div>
发表于 2015-09-16 11:49:24 回复(0)
str,ch不定义成static真的大丈夫?
发表于 2015-09-13 11:50:51 回复(0)
good and gbc
发表于 2015-08-11 14:00:05 回复(0)
因为str是在内存中 new出一个string ,所以每次调用str时,都会在内存中分配一段新的地址,所以当System.out.print(ex.str + ” and ”)执行时,str是新new出的str,而不是原来的str了,所以打印结果为goodandgbc
发表于 2015-08-08 22:31:13 回复(1)
Stringstr 格式不对 若格式正确为String str ,答案为goodandgbc 因为 String 是形参另一个字符串对原字符串无变化 而char是指针 修改的是其本身
发表于 2015-08-08 21:18:21 回复(0)
7c头像 7c
test okandgbc
发表于 2015-07-27 21:53:01 回复(0)
首先String 这个类很特殊,它的特殊之处在于修饰的它的居然是是final,从而导致了String这个类在任何情况下,其内容都不能被改变。所以String 只能被当作引用类型来使用,故而当chang()这个方法接收它时,其String的内容不会被change方法体内的操作而改变。至于char[],这个数组,chang()方法体类操作的是它的元素,所以''g''会把''a''给覆盖掉。
发表于 2015-07-20 17:57:47 回复(0)
goodandgbc
编辑于 2015-07-17 22:51:56 回复(0)
参考答案输出结果:goodandgbc
发表于 2014-11-14 20:45:33 回复(0)