Monday, February 2, 2015

Hello Name

2. Write a program in java to print "Hello Name" where Name will be passed as command line argument at run time.

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

After compilation, run this program as follows and watch the output in next line:
prompt>java User Amit
Hello Amit

Caution: if nothing is passed from command line argument, it will show an Run-time Exception as ArrayIndexOutOfBoundsException, as there will be no element in the String array 'args'.

Back to Exercise

No comments:

Post a Comment