Nearby lessons

89 of 125

Java - Command Line Arguments

📌 What You Will Learn
  • How to set up Java (PATH) on your computer
  • How to write, save, compile and run your first program
  • The meaning of every word in the main method
  • The rules about class name and file name
  • How to fix the common errors a beginner faces

Command Line Arguments is a core concept of the Java language. This lesson explains A Small Practice Program with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

A Small Practice Program

Try to predict the output of this program before running it:

Compile it, then run it like this (notice the values given after the class name):

Example01
JCode Cell
1class Practice {
2 public static void main(String[] args) {
3 System.out.println("My name is: " + args[0]);
4 System.out.println("My city is: " + args[1]);
5 }
6}

A Small Practice Program

The values Rahul and Delhi passed on the command line go into the args array. This is how the String[] args parameter works.

Example02
JCode Cell
1C:\> javac Practice.java
2C:\> java Practice Rahul Delhi
Output
My name is: Rahul My city is: Delhi
📝 Key Takeaways
  • Six steps: install JDK → choose editor → write program → save file → compile with javac → run with java.
  • File name must match the public class name exactly, and must end with .java.
  • javac creates a .class file (bytecode); java runs it.
  • public static void main(String[] args) is the entry point of every program.
  • Comments (//, /* */, /** */) are ignored by the compiler and are used for documentation.
  • Most beginner errors are due to PATH, folder location, or spelling mistakes.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2