×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Topic

  • 工作学习 / IT技术讨论 / A Java question for fun A Java question for fun
    Javanese,

    I wrote a small Java class called Hello. In its main() method, I wrote the following code:

    System.out.println("HELLO "+args[0].toUpperCase());

    now if I run my small class as follows:

    java Hello

    What result will I get? Will I get a NullPointerException? Why?

    Can anybody offer an answer here?
    • Please try java Hello test cause in Java args[0] = "test"
      • Thank you, fly_eagle Thank you, fly_eagle
        本文发表在 rolia.net 枫下论坛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----
        ArrayIndexOutOfBoundException.
        The example has no opportunities to throw
        NullPointerException.

        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.exit();
        }
        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
        • Thanks,pls come in. Thanks,pls come in.
          Jobber, sorry about I wasn't quite clear on your question,then gave your my answer.Maybe
          you felt a bit funny.Anyway thanks to your very clear answer. Sometime I just know what will happen,
          but I don't know why it will happen.Appreciate your answer, I am sure that would be a great help for me
          to improve my java skill. A new java guy!
          • To Fly_Eagle To Fly_Eagle
            本文发表在 rolia.net 枫下论坛Fly_eagle, you are too humble. No sorry to say unless you have stepped on my toes. :-)

            Yes, I know you have figured the problem
            in my old code. The complexities come about as the code become lengthier. This is
            why I always explain the basic prinicple by cooking up some small examples.

            Understanding error messages is very very important. Just give you an example.
            One of my colleagues told me his Weblogic
            Server is wrong because he could not start it up. He has no habit to read server logs...
            I asked to restart server again and show me the servlog. I found it is a java,net.BindException. Then, I told him
            he probably has already started WebLogic server. He said no. But I found from his taskbar a MS-DOS containing weblogic logs
            This is a server instance he started one day ago.... Here there is no coding. The important thing is that one needs to understand BindException. (Port no 7001 has been occupied by the old instance so my colleague could not start up a new one.)


            Many C++ programmers think Java is a piece of cake. This is not true. Java syntax is similar to C++ one but it is not the whole story. We need to accumulate experiences in debugging. trouble-shooting....更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • Thanks.
        • Hello jabber, i tried ur above codes. Hello jabber, i tried ur above codes.
          when i compiled the "user-friendly" version, there was an Error message for statement System.exit(); ---
          exit(int) in java.lang.System cannot be applied to ()
          I tried different int in the braces and all of them worked well, but i don't know what the paremeter means here..
          • Error level as a Java Application fails Error level as a Java Application fails
            You are so careful... This means you can teach yourself.

            System.exit(0) means that application ends normally.

            System.exit(1) means that application ends
            becaue of error.

            Java appication is just one process on your machine. Your system may monitor and catch the status of your Java application. In Unix system, there are various error levels...
            1, 2, 3,,.. The system can judge what happened with your application by catching these number.

            However, if we just do small exercises, these numbers do not matter because we do not catch them.
            • thanx jabber thanx jabber
              it's difficult for me to teach myself since my only programming experience is the simplest BASIC programming when i was in elementary school and foundamental javascript i'm learning in the class now. i always stuck into the mud of those trifles and the progress is slow. Anyway i have time.
              This forum is really helpful. i always check some old posts over and over no matter whether i can understand what u guys are talking about. i'm happy that i can understand more and more now.
    • the result is right,because you use "args[0].toUpperCase()".but when you run the program,you don't append a parameter,so args[0] == null, call null.toUpperCase will generate null exception.