OO Programming with Java


Hello World !

Command line accepts arguments that will be passed to the program by the way of the args parameter. The first parameter being indexed by 0 (see args[0]), the second by 1, etc. String can be concatened with the + operator.

  1. Change the code with:
System.out.println("Hello "+args[0]+" !");

Then (recompile and) run with:

java -cp . Hello.java John
> Hello John !

As a remark, an array can be used into a loop statement illustrated below:

for(String arg : args) // (:) means 'belong to'
  System.out.println(arg);

Another illustration of a for loop is:

String message = "welcome";
for(char c : message.toCharArray())
  System.out.print(c);

5 - 17