top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

    Get Free Article Updates

Facebook Login
Site Registration

Java Command Line Arguments

0 votes
51 views

Java Command Line Arguments

A user can pass any number of arguments to a Java applications at runtime from the OS command line. The main() method declares a parameter named args[] which is a String array that accepts arguments from the command line. These arguments are placed on the command line and follow the class name when it is executed. For example,

Java EmployeeDetail Roger Smith Manager

Here, EmployeeDetail is the name of  a class and Roger, smith, and manager are command line arguments which are stored in the array in the order that they are specified. When the application is launched, the runtime system passes the command line argument to the application’s main() method using a String array, args[]. Note, that the array of strings can be given any other name. The args[] array accepts the arguments and stores them at appropriate locations in the array. Also, the length of the array is determined from the number of arguments passed at runtime. The arguments are separated by a space.

The basic purpose of command line arguments is to specify the configuration information for the application.

As already learnt, that the main() method is the entry point of  a Java program, where objects are created and methods are invoked.

The staticmain() method accepts a String array as an argument as depicted in Code Snippet below:-

public static void main(String[] args){}

The parameter of the main() method is a String array that represents the command line argument. The size of the array is set to the number of arguments specified at runtime. All command line arguments are passed as string.

To run the program with command line arguments at command prompt, do the following:

  1. Open the command prompt.
  2. Compile the java program by writing the following statements 

Javac CommandLine.java

  3.Execute the program by writing the following statement:

Java CommandLine Roger Smith Manager (pass the argument)

posted Dec 30, 2017 by Jon Deck

  Promote This Article
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button


Related Articles

Wrapper Class :

  • Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.
  • Sometimes it is required to create an object representation of these primitive types.
  • These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.
  • To satisfy this need, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class.
  • Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.
  • The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.

These classes offer a wide array of methods that allow to fully integrate the primitive types into Java’s object hierarchy.

 

Wrapper classes for converting simple types

Simple Type

Wrapper class

booleanBoolean
charCharacter
doubleDouble
floatFloat
intInteger
longLong

Converting primitive numbers to Object numbers using constructor methods

Constructor calling

Conversion Action

Integer IntVal = new Integer(i);Primitive integer to Integer object
Float FloatVal = new Float(f);Primitive float to Float object
Double DoubleVal = new Double(d);Primitive double to Double object
Long LongVal = new Long(l);Primitive long to Long object

 

NOTE : I, f, d and l are primitive data values denoting int, float, double and long data types. They may be constants or variables.

 

Converting Object numbers to Primitive numbers using typeValue() method

Method calling

Conversion Action

int i = IntVal.intValue();Object to primitive integer
float f = FloatVal.floatValue();Object to primitive float
double d = DoubleVal.doubleValue();Object to primitive double
long l = LongVal.longValue();Object to primitive long

Converting Numbers to Strings using toString() method

Method calling

Conversion Action

str = Integer.toString(i);Primitive integer i to String str
str = Float.toString(f);Primitive float f  to String str
str = Double.toString(d);Primitive double d to String str
str = Long.toString(l);Primitive long l to String str

Converting String Object in to Numeric Object using static method ValueOf()

Method calling

Conversion Action

IntVal = Integer.ValueOf(str);Convert String into Integer object
FloatVal = Float.ValueOf(str);Convert String into Float object
DoubleVal = Double.ValueOf(str);Convert String into Double object
LongVal = Long.ValueOf(str);Convert String into Long object

Converting Numeric Strings to Primitive numbers using Parsing method

Method calling

Conversion Action

int i = Integer.parseInt(str);Converts String str into primitive integer i
long l = Long.parseLong(str);Converts String str into primitive long l

NOTE : parseInt() and parseLong() methods throw a NumberFormatException if the value of the str does not represent an integer.

To know about wrapper class in java visit the following link

Java Wrapper Class

READ MORE
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...