Androidberry logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Commandline Arguments in Java


Java Commandline Argument, Command Prompt

What is the commandline arguments mean in java?

command line arguments are parameters that are supplied to the application program at the time of starting its execution. They must be supplied at the time of its execution after the filename.

we can pass any number of commandline arguments while launching an application

Normally we execute the program by using below commands
  • javac filename.java
  • java filename
How to pass commandline arguments?
  • javac filename.java
  • java filename argument1 < space > argument2 < space >... argumentN

Everything you pass from commandline argument is considered as inString format.

Lets see below program to understand best use of commandline argument

Example

Qn. Develop a program to print string using commandline argument

class First
{
   public static void main(String args[])
   {
     System.out.println("You have passed : "+args[0]);
   }
}

Compile above program by using following steps

  • javac filename.java
  • java classname AndroidBerry.com

Output

You have passed : AndroidBerry.com

How to pass multiple arguments?

By default the arguments are considered as String. We have need to convert them into the respective datatype by using type casting.

If you want to store integer value into variable we use int a = Integer.parseInt(arg[0]);

Example

Develop a program to find addition of two numbers using commandline argument.

NOTE : Assume values are 10 and 20

class Addition
{
    public static void main(String args[])
    {
	int a = Integer.parseInt(args[0]);
	int b = Integer.parseInt(args[1]);

	System.out.println("A : "+args[0]);
	System.out.println("B : "+args[1]);

	System.out.println("Addition = "+ a+b);
    }
}

Compile the program using

  • Step1 - javac Addition.java
  • Step2 - java Addition 10 20

Output

A : 10
B : 20
Addition = 30

How does multiple argument work?

  • In public static void main(String args[]) : the args is an object of type string which contain empty brackets as [ ]. this means that args[] able to get any number string arguments because we have not specified any size like args[10] etc.
  • The very first value is received into the args[0] index and second value is in args[1]. thats why we have specified the indexes of each value while converting into integer.
  • Integer is an object which contains the method (function) called parseInt() : which is used for conversion of string into integer type

How to convert String into other types?

java allows us to convert the string into following datatypes

  1. Integer - Integer.parseInt(value);
  2. Float - Float.parseFloat(value);
  3. Double - Double.parseDouble(value)

For Practice
  • Try to pass float and double value instead of integer
  • Change the argument name to your own name
    Example String args[] into String argument[]
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo