Java Program Arguments
Pass words on the command line when you start a program, and read them inside main. In Flag.java, keep the public class Flag and the parameter String[] args. Print args[0] on the first demo. On the second demo print args.length, then args[0]. Compile with javac Flag.java. Run java Flag oak so the first word after the class name is oak. The first screen shows oak. Leave optional Generics for later pages. This lesson is command-line words in args, not a Scanner conversation and not a Mini Project label.
Read the first word after the class name
Open Flag.java. Keep the public class name Flag. Delete leftover Box mini-project Scanner demos, leftover Build compiling prints, leftover Recap summary lines, and leftover Dice Random rolls if they bury the arguments story. This program must not wait for keyboard Scanner input.
Inside Flag.main, keep the parameter list as String[] args and print the first word:
System.out.println(args[0]);
From the folder that holds Flag.java, run javac Flag.java. Then run java Flag oak — not java Flag.java oak. The class name is Flag. The word oak is the first program argument. It lands in args[0]. The screen shows oak.
public class Flag {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
Walk the run once. javac writes Flag.class. java Flag oak loads the class and fills args with one element: "oak". main starts. args[0] is "oak". println writes oak. Nothing asks item:. Nothing prints mug 4. Nothing prints ok from Build.
Say the beginner rule while you look at the command: words after the class name become args. Index 0 is the first word. Do not invent Generics demos on this page. Optional Generics waits after the core path. Do not type java Flag alone and then read args[0] — that empty array has no first word and the run can throw.
If you still see mug 4 from a leftover Box file, switch to Flag.java, compile, and run java Flag oak. If javac complains about the class name, keep public class Flag matching Flag.java.
Count the words, then print the first
One argument proves args[0]. A second demo shows args.length together with the first word. Keep the same public class Flag. Print two lines:
System.out.println(args.length);
System.out.println(args[0]);
Compile again with javac Flag.java. Run java Flag oak pine. The screen shows 2, then oak. The length is 2 because two words follow the class name. The first word is still oak. The second word pine sits in args[1] but this demo does not print it.
If you run java Flag oak on this two-line demo, the screen shows 1, then oak. The length follows how many words you typed after Flag.
public class Flag {
public static void main(String[] args) {
System.out.println(args.length);
System.out.println(args[0]);
}
}
Compile with javac Flag.java and run java Flag oak pine again. Walk the run once. args.length is 2. args[0] is "oak". The prints write those two lines. Nothing waits for Scanner. Nothing joins mug and 4.
Keep the two demos as two teaching shapes. First prove one argument prints. Then prove length plus the first word. Both shapes share the same class name and the same idea: words after java Flag fill args.
If you write java Flag.java oak, you are using the wrong run shape for this course path. Keep java Flag plus the words. If you forget to compile after an edit, a stale .class may show an old print shape — compile again, same Compiling habit.
args is not Scanner and not Mini Project
Mini Project asked you to type answers while the program waited. Program Arguments asks you to pass words when you start the program. Scanner reads from System.in after the process starts. args is already filled before main runs. Do not mix those two input styles in one beginner file today.
oak is only a teaching word. It is not a Random seed and not a Summary map row. When a leftover Box demo still waits at item:, that file is doing Mini Project work. Switch to Flag and pass oak on the command line.
Without at least one word, reading args[0] fails. With words present, indexes start at 0. args.length tells you how many words were passed. Keep those three facts together.
Core path checkpoint
You already built classes with main, compiled with javac, ran with java, stored values, decided with if, walked loops, called methods, used objects and lists, caught errors, read files, and shaped inheritance. Mini Project joined two typed answers into mug 4. Program Arguments shows another way to feed a program: words on the launch line.
This page ends the core Java path on this course. Optional pages such as Generics come after, when you want them. They are not required to finish the core path. Do not treat this arguments lesson as a place to invent those optional topics.
Keep one public Flag. Keep String[] args on main. Keep the run command as java Flag plus words. Keep Mini Project Scanner demos off this page when you practice arguments.
Folder checks before you finish core
List the folder after a successful compile. You should see Flag.java and Flag.class together. Edit the source, save, run javac Flag.java, then run java Flag oak or java Flag oak pine. A missing .class means compile first. A wrong folder means the launcher cannot find Flag.
If the screen shows nothing, confirm main still prints. If the run throws because args is empty, add a word after the class name. If two public classes share one file, keep only Flag public.
Do not paste Box Scanner prompts into Flag to “enrich” the page. Do not rebuild Build's ok prints here. The teaching job is command-line words only.
Before optional Generics
You can keep public class Flag, read String[] args in main, print args[0] for java Flag oak, print args.length then args[0] for java Flag oak pine, compile with javac Flag.java, and run without the .java ending on the class name. The core path ends on this page. Optional Generics waits if you continue later.
Keep the Flag demos for a clear arguments pair. Keep Box Mini Project files off this page when you practice args. Keep Build compiling demos off this page when you practice args. Keep Generics work off this page until an optional lesson. Those habits are the checkpoint before you leave the core path. When the one-word run shows oak or the two-word run shows 2 then oak, Program Arguments did its job for this lesson and the core Java path is complete.
One last pass through the folder: one public Flag, String[] args on main, no Scanner wait, no Random, no LocalDate, compile then run with words after the class name. Run javac Flag.java and java Flag oak once more. If the screen still shows oak, the Program Arguments shape is solid and the core course can mark complete while optional pages stay available later. Prefer one clear arguments habit per read so each pass teaches the same map.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top