This tutorial takes up where the Hello World! in Java left off. Our next step will be to allow the user to supply their name at runtime to personalise the “Hello World!” greeting. We will use the final code from that tutorial (see below) as a starting point in our new program, HelloWorldArg.
In this tutorial:
Required knowledge:
1. Arguments
The following program simply outputs a message to the screen:
class HelloWorld { public static void main(String[] arguments) { // this is just a comment System.out.print("Hello World!"); } }
An argument is a value passed to a function when the function is called.
To add to the usefulness of the program, we are going to allow the user to enter their name as an argument to our program at runtime to personalise the greeting!
To enter user input at runtime, we use the following syntax at the Command Prompt: java NameOfProgram arguments1 arguments2 ...
To output the information in the program, we access the argument values using arguments[0]
, arguments[1] etc
The final code is therefore:
class HelloWorldArg { public static void main(String[] arguments) { // this is just a comment System.out.print("Hello World!\n"); System.out.print("My name is " + arguments[0]); } }
LINE 4: “\n
” is an Escape Character in Java that is used to enter a new line in the output. [2]
LINE 5: the +
concatenates (joins) 2 strings together
Are you remembering to compile and run your code to test for errors?.
2. Run
To run this program at the Command Prompt:
java HelloWordlArg Fox
or, for your full name including a space:
java HelloWorldArg "Mister Fox"
3. jGRASP
If you run a program that requires arguments at runtime in jGRASP:


- Select the Build menu
- Select the Run Arguments option
- Supply the argument(s) in the Run Arguments field
4. Next steps
What happens if you run the program without supplying an argument? What happens if you supply more than one word?
Wouldn’t it be great if the message popped up in a window? Even better: a pop-up window for the user input?
References:
- Specifying Command-Line Arguments in jGrasp. (2023). Retrieved 29 January 2023, from https://kyledewey.github.io/comp110-fall17/resources/jgrasp_command_line_arguments/
- Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings) . (2023). Retrieved 30 January 2023, from https://docs.oracle.com/javase/tutorial/java/data/characters.html