Java Essential Training (2011)

Passing arguments to the application


Java Essential Training (2011)

with David Gassner

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Passing arguments to the application

When you create a Main class, that is a class that you want to start up when the application begins. It always has a main method when you are running Java from the command line or the console. The syntax for this can differ from one platform to another. For example, you don't use a main method in a Java Servlet, you use other kinds of methods, but when you are running from the command line, it's always called main. The signature or syntax of the main method always looks like this: it has the keywords public, static, and void.
Expand all | Collapse all
  1. 10m 8s
    1. Welcome
      1m 3s
    2. Is this course for you?
      5m 35s
    3. Using the exercise files
      3m 30s
  2. 31m 25s
    1. The history of Java
      5m 19s
    2. Java compilation and syntax
      8m 55s
    3. Understanding the principles of Java
      8m 28s
    4. Choosing a development environment
      8m 43s
  3. 19m 52s
    1. Installing Java on Windows
      6m 42s
    2. Installing Eclipse on Windows
      3m 19s
    3. Exploring Java on Mac OS X Leopard and Snow Leopard
      2m 27s
    4. Installing Java on Mac OS X Lion
      4m 14s
    5. Installing Eclipse on Mac OS X
      3m 10s
  4. 46m 10s
    1. Creating a Hello World application
      11m 7s
    2. Exploring the Eclipse IDE
      8m 55s
    3. Compiling and running from the command line
      8m 2s
    4. Passing arguments to the application
      8m 17s
    5. Using the Java API documentation
      4m 5s
    6. Memory management and garbage collection
      5m 44s
  5. 58m 57s
    1. Everything is an object
      5m 59s
    2. Declaring and initializing variables
      9m 15s
    3. Working with numbers
      8m 32s
    4. Converting numeric values
      6m 40s
    5. Understanding operators
      7m 58s
    6. Working with character values
      5m 14s
    7. Working with boolean values
      5m 13s
    8. Outputting primitive values as strings
      5m 33s
    9. Creating a simple calculator application
      4m 33s
  6. 53m 41s
    1. Writing conditional code
      5m 35s
    2. Using the switch statement
      8m 50s
    3. Repeating code blocks with loops
      7m 35s
    4. Creating reusable code with methods
      6m 31s
    5. Declaring methods with arguments
      5m 42s
    6. Overloading method names with different signatures
      5m 53s
    7. Passing arguments by reference or by value
      5m 35s
    8. Creating a more complex calculator application
      8m 0s
  7. 20m 31s
    1. Using the String class
      5m 44s
    2. Building strings with StringBuilder
      3m 34s
    3. Parsing string values
      3m 20s
    4. Working with date values
      7m 53s
  8. 20m 44s
    1. Understanding compile-time vs. runtime errors
      4m 5s
    2. Handling exceptions with try/catch
      4m 55s
    3. Throwing exceptions in methods
      2m 50s
    4. Using the debugger
      8m 54s
  9. 32m 22s
    1. Using simple arrays
      4m 47s
    2. Using two-dimensional arrays
      6m 17s
    3. Managing resizable arrays with ArrayList
      7m 14s
    4. Managing unordered data with HashMap
      6m 5s
    5. Looping through collections with iterators
      7m 59s
  10. 52m 3s
    1. Understanding encapsulation
      5m 59s
    2. Creating and instantiating custom classes
      8m 8s
    3. Organizing classes with packages
      6m 47s
    4. Creating and using instance methods
      6m 52s
    5. Storing data in instance variables
      6m 56s
    6. Using constructor methods
      5m 40s
    7. Managing instance data with getter and setter methods
      8m 26s
    8. Using class variables and Enum classes
      3m 15s
  11. 41m 15s
    1. Understanding inheritance and polymorphism
      9m 12s
    2. Extending custom classes
      9m 1s
    3. Overriding superclass methods
      3m 8s
    4. Casting subclass objects
      5m 3s
    5. Understanding interfaces and implementing classes
      4m 2s
    6. Creating your own interfaces
      4m 14s
    7. Using abstract classes and methods
      6m 35s
  12. 32m 17s
    1. Managing files with the core class library
      7m 46s
    2. Managing files with Apache Commons FileUtils
      7m 32s
    3. Reading a text file from a networked resource
      7m 52s
    4. Parsing an XML file with DOM
      9m 7s
  13. 17m 39s
    1. Creating your own JAR files
      4m 54s
    2. Understanding the classpath
      5m 2s
    3. Documenting code with Javadoc
      7m 43s
  14. 47s
    1. Goodbye

please wait ...
Watch the Online Video Course Java Essential Training (2011)
7h 17m Beginner Dec 14, 2011

Viewers: in countries Watching now:

Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.

Topics include:
  • Understanding the history and principles of Java
  • Installing Eclipse and Java
  • Compiling and running from the command line
  • Managing memory and performing garbage collection
  • Declaring and initializing variables
  • Writing conditional code
  • Building and parsing strings
  • Debugging and exception handling
  • Using simple arrays
  • Creating custom classes
  • Working with encapsulation, inheritance, and polymorphism
  • Managing files
  • Documenting code with Javadocs
Android Java Eclipse
David Gassner

Passing arguments to the application

When you create a Main class, that is a class that you want to start up when the application begins. It always has a main method when you are running Java from the command line or the console. The syntax for this can differ from one platform to another. For example, you don't use a main method in a Java Servlet, you use other kinds of methods, but when you are running from the command line, it's always called main. The signature or syntax of the main method always looks like this: it has the keywords public, static, and void.

Public means that the method can be called from anywhere. Static means that you don't have to create an instance of the class. And void means that you are not returning anything from the class. Now, I'll talk more about these keywords in later videos, but for now what you need to know is that these three keywords are required as part of the main method declaration. The other thing that the main method has to have is this syntax, String, open/closed bracket, and then the name of the variable, which by default is set to args by Eclipse, but could be anything.

This is a value that's passed in by the Java Virtual Machine as the application starts up. The brackets means that the argument is an array, an ordered collection of values. And these are the command line arguments or values that are passed into the application when it starts up. Now, you can pass arguments in from Eclipse or from the command line. It will make most sense if we start from the command line though. So open up a command window; on Windows use the cmd command and on Mac use terminal, and then change to bin folder underneath the project arguments that I've imported into Eclipse.

I'll type cd Desktop\Exercise Files \03_GettingStarted\Arguments\bin. List the contents of the directory using dir on windows or ls on Mac, and you should see that there is a class there called Main.class. Now, I am going to run the application. First I'll clear my screen. Then I'll type java Main. And I don't see any output, that's because my source code for the moment doesn't have anything in the main method.

I'll switch back to the command window using Alt+Tab on Windows or Command+Tab on Mac, and this time I'll type java Main arg1 arg2. Again, there is no output, but let's add some code to the application to detect and report the arguments. I'll go back to the main method. The args variable is an array, and in Java every array has a property called length, which returns numeric value indicating how many items there are in the array.

So I am going to add a little bit of output that looks like this. System.out.println, and then I'll put two values together, one string and one number. The string will be Number of args: , then I'll move the cursor after the last double quote and put in a Plus operator and then args.length. The length property is numeric. When you take a string and a numeric value and put them together using the plus operator, that's called concatenation, and in Java the numeric value will be automatically converted to a string.

I'll talk about the mechanics of that in a later video. I'll finish the statement with the semicolon at the end and I'll Save my changes. Now, by default when you Save your changes to your code, Eclipse automatically recompiles the application for you. This is because under the Project Menu there is an option labeled Build Automatically. It's checked by default. If you want to you can uncheck that option and only compile or build when you say so, but I'll keep it on, so whenever I Save my changes the .class files are recreated for me.

So now I can switch back to the command window and I'll repeat the same command by pressing the Up Arrow and then either Enter or Return, and I should get the output, Number of args: 2. So now I know that I am receiving the arguments from the command line and that I have exactly the number that I typed in. Let's go back to the code and add a little bit more. Place the cursor after that initial statement and press Enter or Return a couple of times to make a little bit of blank space in your code. Now, I am going to add a little bit of code that loops through the arguments and report the value of each argument in turn.

This takes a little bit more complex code, but the good news is that Eclipse will write the code for you. Try this, type for, the word for, then hold down the Ctrl key and press Space. This keyboard shortcut is the same on both Windows and Mac. It brings up a list of available options. Choose the first option, for - iterate over array by pressing Enter or Return, and Eclipse adds a bunch of code that will loop through the array. Now, I am not going to describe the exact syntax of this code right now, we'll save that for later, but all you need to know is that when you use a for command and you provide all this information, you are looping through the contents of the declared array, in this case args, and you can deal with each item of the array in turn.

I'll click into the for loop, between the braces, and once again I'll use System.out.println and then I'll pass in the value of the current argument using the syntax args, open bracket, i, closed bracket, and I'll remember to put in my semicolon at the end. i is a variable that was declared inside the for loop, when I said int i = 0, it's initial value is 0, and each time through the loop it's incremented by 1.

The first item in the array is item 0, the second one is item 1, and so on. I'll press Command+S or Ctrl+S to save my changes, switch back to the command line, and pressing the Up Arrow run the application again. And now I see the total number of arguments and I see the value of each argument output one at a time. When you pass arguments from the command line, each argument is delimited and separated from the other by a space character, but if you want to include a space character and a value, just wrap the value inside quotes.

So I'll press the Up Arrow to bring that command back again, but I'll move the cursor over and add quotes around the two words, arg1 and arg2. Then I'll run the application again, and this time I am told I only have one argument, and the value of the argument is the entire string. So that's how you pass in the arguments from the command line. How would you do the same thing directly in Eclipse? I'll go back to Eclipse and I'll go into my Run Configurations. I'll go to the Toolbar, pull down the Run Menu and choose Run Configurations.

I'll add a new Run Configuration by clicking Java Application and clicking Plus, and the new name is going to be Main (1). And over here I'll click on the Arguments tab. I can pass in as many arguments as I want. I'll list these arguments one per line, so this will be arg1 and arg2 and arg3 and arg4. Then I'll click the Run button, and down here in the Console View I'll expand the Console so we can see all the output.

I see Number of args: 4 and then each of the arguments listed one at a time as the code loops through and deals with each item in turn. So that's the purpose of the args parameter that's passed into the main method. It allows you to receive and process values that are passed into your Java application at runtime, either on the command line working in the command application in Windows, or in terminal on Mac, or from within Eclipse from a Run Configuration that's set up for this purpose.

Find answers to the most frequently asked questions about Java Essential Training (2011) .

Expand all | Collapse all
please wait ...
Q: Can I safely use Java 8 with this tutorial or do I have to be using Java 6 specifically?
A: Yes, you can use Java 8 with this course.
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed Java Essential Training (2011).

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.