下列程序的输出结果是(  )。 public class Test { public static void main (String[] args) { String s="hello";

admin2012-12-13  30

问题 下列程序的输出结果是(  )。    public class Test {        public static void main (String[] args) {            String s="hello";            s.replace (’r’,’m’);            System.out.println(s);        }    }

选项 A、hello
B、HELLO
C、hemmo
D、HEMMO

答案A

解析 String类的replace (char oldChar,char newChar)函数的作用是返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar而生成的。返回的是新字符串,但是原字符串变量的值并未发生改变。因此,输出的是“hello”而不是“hemmo”。如果替换语句换为: s=s.replace(’l’,’m’);,则输出“hemmo”。
转载请注明原文地址:https://jikaoti.com/ti/jq80FFFM
0

最新回复(0)