Play With args[]

Welcome to mil-notes. In this post, we are going to play with args[] in Java. Yes, commonly, when we write a main method, we usually write these code:

public static void main(String[] args) 
or
public static void main(String args[])

Actually, What does the those Java code mean?
Based on Oracle Java Docs, it called as Command Line Arguments. This allows the user to specify configuration information when the application is launched. Now, we're going to play with it, yes just to make you more understand.

See for output example below:

Understand?, ok if you want to know how to do it, open your text-editor and start to code.
To begin, write these code.

After that, save it as CommandLineExample.java. Remember where you save it, because we are going to compile it. To compile it, we have to navigate to where we save the java file through command prompt / terminal and then compile it. To compile, type these command:

javac CommandLineExample.java

See the example below

If there's no error, we can run it right away. But, if there's still an error, check your code again. To run, type these command:

java CommandLineExample [ Arguments ]

example:


Get it? interesting? Do you want to expand it? If you want, wait for my next post.

 - FIN -

[ SOURCE CODE ]

  1. CommandLineExample.java


[ RESOURCE ]

Command-Line Arguments. http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html. Oracle: Java Documentation. Accessed at May 27th 2017.



Comments