Thank you, fly_eagle

jabber (jabber)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Fly_eangle, thank you for your response.

My code is as follows.

class Hello{
public static void main(String args[]) {
System.out.println("HELLO" + args[0].toUpperCase());

My question is: What happens if I run this example by issuing a command "java Hello"?

I have ever asked about 10 Java guys for this question. The answer is discouraging. At least 5 people said NullPointerException.

The fact is that ArrayIndexOutOfBoundException will be thrown. The reason is as follows:
Because I did not give any on-line parameter,
args[] has a length of zero. Since its length is
zer0, it doesn't have No. 0 element. As
we access args[0], we run into trouble----
The example has no opportunities to throw

If you are going to take Sun's programmer certificate test, this question may help you.

This is a good place to explain what does "robust: means. In the job advertisement, one often sees the word
"robust". Right now, my Hello class is
not robust because it fails as I
forget to input on-line parameter.

to make it ROBUST, now I can modify my code as follows:

class Hello{
public static void main(String args[]) {
if(args.length!=0) {
System.out.println("HELLO" + args[0].toUpperCase());

However, the above code does not print anything if I forget to input the parameter.
So it is not user-friendly. To make it
USER-FRIENDLY, I can modify my code again:

class Hello{
public static void main(String args[]) {
if(args.length==0) {
System.out.println("Usage: java Hello <name>");
System.out.println("HELLO" + args[0].toUpperCase());

Of course, my code is not best-organized. But the basic idea is already there.

Some guys always say they don't know how to write code. My answer is always "what do you want to do? ". If you don't know what goal
you have, no one can help write code.

On the other hand, some Java experts often
tell the novice "this is easy, and that is simple...". I see nothing is simple in the IT industry. Everything is not as trivial as it looks to be.
更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
2000-9-26 -04:00
This post has been archived. It cannot be replied.
Page address has been copied. To share, click to copy page address.
Share Online by QR Code

Back To Topic: A Java question for fun

Back To Forum: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论