Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

java - Command Line Arguments vs Input - What's the Difference?

What is the difference between command line arguments and input?

Given some program running:

$ java JavaProgram 4 5
Hi! give me some input!
6
now give me some more input!
7

In this example 4 5 are command line arguments and 6 7 are inputs.

Both command line arguments and input, seem to supply the same functionality of taking some varying data from the user. What do we need both for?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Command line arguments and input are two different things.

Command line arguments are given to the application that is being run, before it is run. Let's look at an example:

$ java JavaProgram 30 91

First we give the app JavaProgram the command line arguments 30, 91, only then do we hit Enter and run it as a Java program.

Meaning:

  1. The command line arguments are a part of a specific invocation of the application. (we can give it other command line arguments in other times we run it)
  2. Command line arguments are given to a program BEFORE it starts running.

Contrary to that, input can be given to an application during its run, because it can only ask for input after it started running. For that reason, we can print some text to the user before asking for input, indicating what input we are expecting for, etc.

But we can't do it with command line arguments, as an app that isn't running - can't do anything, and in particular can't print messages to the user.

Command line arguments are taken once - either zero, three, ninety, or whatever number of command line arguments. (actually there's a limit to that number, but it's very big and irrelevant)

Input can be taken any number of times. For that reason, input can be interactive - the system can take input, then respond according to it, then take more input, etc. Command line arguments are taken once, therefore can not be used to manage any interactiveness.

All the above, narrows the conversation "what’s the difference between command line arguments and input?" to the very specific cases where we wish to take input once, and do not wish to print anything prior to that. Even in that case, a command line argument would still be given before the program starts running, comparing to input which would be given after the program starts running.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...