Java programs can read values typed after the class name and use them to choose what appears in the console. A simple conditional can match a pair of strings, print a message such as “Hello!”, and skip other branches—small logic, clear results, and a useful glimpse of how Java handles input.

Multiple Choice

Which command prints Hello! when run with the arguments open and someone?

When a program uses command-line arguments to decide its output, it typically checks the provided values and takes a specific path for those matches. With the two arguments open and someone, the code has a branch that prints the literal string Hello! and then stops. So the exact output for these inputs is Hello!, which is why that option is the correct one. The other possibilities would require different input patterns or a different code path (such as assembling a sentence, signaling an error, or producing no output).

In Java, a program can respond to the words typed after its name when it starts. These words are called command-line arguments. They let a user choose what a program should do without changing its source code. A small command-line tool might print a greeting, show a help message, or report an error, all based on the arguments it receives.

This idea is a useful part of learning Java at FPT Software (Fsoft) Academy, where students encounter the building blocks behind larger applications. The key is simple: Java gives the program the input, and the program checks that input before choosing what to do.

What are command-line arguments?

When a Java application starts, its main method can receive a list of strings:


public static void main(String[] args) {

// Program code goes here

}

The args variable is an array of String values. If a program is launched with two words after its name—open and someone—then args contains those two values:


args[0] = "open"

args[1] = "someone"

Array positions start at zero, which can feel a little odd at first. The first argument is at index 0, the second at index 1, and so on. That detail matters: reading args[1] assumes that a second argument exists. If it doesn’t, the program may throw an ArrayIndexOutOfBoundsException.

Command-line arguments are always received as strings. Even if someone types 42, Java initially sees the text "42". To use it as a number, the code must convert it, for example with Integer.parseInt(args[0]).

How Java chooses what to print

A program often compares arguments with expected words using an if statement. For example:


public class Greeting {

public static void main(String[] args) {

if (args.length >= 2

&& args[0].equals("open")

&& args[1].equals("someone")) {

System.out.println("Hello!");

}

}

}

When the application receives open and someone, the condition evaluates to true. Java runs the statement inside the braces and prints:


Hello!

The punctuation is part of the literal string. System.out.println displays the text and then moves to a new line. It doesn’t add extra words or explain why that branch ran.

Notice the order of the checks. The program first makes sure there are at least two arguments, then reads their values. Java evaluates && from left to right and stops as soon as a condition is false. This short-circuit behavior helps prevent the program from trying to read a missing array element.

Why use .equals() for strings?

A common early mistake is to compare strings with ==:


args[0] == "open"

That checks whether the two references point to the same object, not whether their text matches. For comparing the contents of strings, use .equals():


args[0].equals("open")

You can think of it like comparing two handwritten notes. The question is whether they contain the same words, not whether they’re the very same piece of paper. In Java, .equals() checks the words.

If capitalization should not matter, Java also provides .equalsIgnoreCase():


args[0].equalsIgnoreCase("open")

That would treat "open" and "OPEN" as a match. Whether this is desirable depends on the program’s rules. A command-line interface may accept either form, while another tool may require an exact spelling.

The order of conditions shapes the result

A chain of branches lets a program respond in different ways:


if (args.length >= 2

&& args[0].equals("open")

&& args[1].equals("someone")) {

System.out.println("Hello!");

} else if (args.length >= 2

&& args[0].equals("close")) {

System.out.println("Goodbye!");

} else {

System.out.println("Unknown command.");

}

Java checks the first condition, then the next one only if the first condition is false. Once a matching branch runs, the remaining branches in the same if/else if chain are skipped. So a program that prints Hello! in the first branch won’t also print the message from a later branch.

This is why output depends on both the supplied values and the exact structure of the code. Two programs can receive identical arguments and still behave differently if their conditions are arranged differently. The source code—not just the command typed—sets the rules.

Output, errors, and missing input

It helps to distinguish three common outcomes:

  • A message is printed: A matching branch calls System.out.println.

  • An error occurs: The program tries an invalid operation, such as reading an array position that doesn’t exist.

  • Nothing is printed: No output statement runs, and there’s no fallback branch that prints a message.

A missing else can make the third outcome possible. For example, if an if condition is false and the program has no else, Java simply continues past the conditional. Silence isn’t automatically an error; it may be exactly what the code describes.

Still, clear feedback usually makes command-line tools easier to use. A program can check the number of arguments first and display a short usage message when input is incomplete:


if (args.length < 2) {

System.out.println("Please provide a command and a name.");

return;

}

The return ends the main method, so the program doesn’t continue into code that expects values to be present. Small checks like this make a program more reliable and its behavior easier to understand.

A closer look at the launch command

In a simple setup, a Java program might be launched like this:


java Greeting open someone

Here, Greeting identifies the class, while open and someone are passed to the program as arguments. The class name itself isn’t stored in args. Only the values that follow it are.

In an IDE, the same values are usually entered in a run configuration. The exact screen varies between tools, but the idea stays the same: the IDE starts the program and passes the configured words to main. That makes command-line arguments useful even when no one is typing into a terminal.

Small details, bigger habits

Conditional output may seem like a tiny topic, but it connects to several important Java habits: checking array length, comparing strings correctly, reading conditions in order, and tracing which branch runs. Those habits carry into larger programs, where input may come from a file, a web request, or a user interface instead of the command line.

For students at Fsoft Academy, the value is in seeing how a few precise lines of code control a program’s behavior. Follow the data from args into each condition, then note which statement runs. It’s a bit like following signposts on a road: once the route is clear, the destination—whether that’s a greeting, a warning, or no output at all—makes sense.