[18 Jan 2012 | No Comment | 286 views]
  • Before using command-line arguments, always check the number of arguments before accessing the array elements so that there will be no exception generated.
  • For example, if your program needs the user to input 6 arguments,

Method 1:

Method 2: Read more…

[18 Jan 2012 | 5 Comments | 26,939 views]

New Java Project

  • Create a new Java Project and name it as “CommandLineArguments

Right click on Package Explorer -New -Project -Select Java Project

New package

  • Create a new Package and name it as “commandline

Read more…

[18 Jan 2012 | No Comment | 1,005 views]
  • Command line arguments are stored as String in the args array. If your application needs a numeric value then it must be converted to the required type.
  • Here’s the code that converts a command-line argument to an integer,

Test Case Read more…

[18 Jan 2012 | No Comment | 1,978 views]
  • In Java, when you launch an application, you can pass command-line arguments to the application’s main method through an array of Strings.
  • Each element in the array is one command-line argument.

Steps to pass command line arguments to the application.

Read more…

[18 Jan 2012 | No Comment | 569 views]
  • Java allows you to pass arguments to an application when the application is launched.
  • The application can accept any number of arguments from the command-line.
  • This allows the user to specify configuration information.
  • The user enters command-line arguments when invoking

Read more…

[18 Jan 2012 | No Comment | 221 views]

Array Creation

There are three ways of creating an array.

Array Length

Length/size of the array can be obtained using


Illegal Array Declaration

You should not specify the size when you declare the array

Array instance/static field

Instance or Read more…

[18 Jan 2012 | No Comment | 404 views]

In Java, Arrays are Objects. Hence, they behave exactly as objects do. If the array is modified in the method then the caller will also see the updated value.

Here is an example,

In main: Before Passing array to methodTest Read more…

[18 Jan 2012 | No Comment | 255 views]

An array can be used almost everywhere.

  • Instance or static field
  • Local or parameter variable
  • Method parameter
  • Method argument
  • Method return type
  • Return expression,
  • etc

Read more…

[18 Jan 2012 | No Comment | 4,265 views]

Using Enhanced For-loop

This is a specialized for loop introduced in Java SE 6 to loop through an array or collection.
It has two parts for(declaration : expression)

declaration newly declared variable of type compatible with the array element.

Read more…

[18 Jan 2012 | No Comment | 657 views]

In this tutorial on Java Arrays, we will see to how to assign an array – both Primitive Array assignment and Reference Array assignment.

Java Primitive Array Assignment

If an array is declared as int (int[]) then you can assign Read more…