一道Java题怎么做,最好详细些~

来源:百度知道 编辑:UC知道 时间:2024/05/10 21:47:11
1. public class test(
2. public static void main(string[]args){
3. string foo = args [1];
4. string foo = args [2];
5. string foo = args [3];
6. }
7. }
And command line invocation:
Java Test red green blue
What is the result?

A.
Baz has the value of “”

B.
Baz has the value of null

C.
Baz has the value of “red”

D.
If an 'onMessage' method returns before committing a transaction the container will throw an exception.

E.
Baz has the value of “blue”

F.
Bax has the value of “green”

G.
The code does not compile.

H.
The program throws an exception.
能不能解释一下啊

不知道代码是不是你自己敲上去的,String类应该是大写开头,另外,在一个main方法里面怎么会定义了三个foo变量呢?

如果代码不是你不小心敲错了,那么答案选G,编译报错,string类不存在,另外foo变量重复定义

如果代码确实是不小心出现上述两个错误,那么答案应该是H

原因:
java Test red green blue
在运行Test时传递三个参数,意味着main方法接受到的参数数组args中有三个元素,调用这三个元素的方式是args[0],args[1],args[2].
可是在你接下来的代码中使用了args[3],这是一个不存在的数组元素,其索引已经超出了范围,所以程序会报一个异常:ArrayIndexOutOfBoundsException,即数组下标越界。

G