Command Line Argument, Object Creation & PrintStream

Command line argument is the argument that is accepted by the main function. In other words, any argument accepted by the main function is called command line argument. In Java main function only accepts an array of String as an argument. In Java, the length of command line argument is always zero by default. However, in case of C & C++ language, it is 1. The program name is itself the 1st argument.

Command Line Argument Example

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(args[0]+ args[1]);

After you compile the program using javac in your command prompt (learn how to set up your local java environment) and run using java Test 11 22 33 44 we would see the output as :

command line argument

In the first two outputs, we are getting the values that we are passing to the command line argument. For the third one, since both arguments are of String type hence concatenation occurs like String A=10; String B=20; So, A+B=1020. In the fourth one, at first 10 and 20 are added up, then the result is added with the ASCII value of  A(65). The result is then concatenated with String B followed by 10 and 20. Here A is a character and B is a String.

Program to convert between two numbers using command line argument

  • Since the arguments, we pass in the command line is a string type by default, we need to first convert the numbers to Integer from String. The same can be achieved with the help of command parseInt()
  • parseInt() a static method present in Integer class which is used to convert a String to Integer.
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int no1=Integer.parseInt(args[0]);
		int no2=Integer.parseInt(args[1]);



We enter the numbers to our command line as 11 and 22 and the output is:

command line argument

Did you know?

The String is a pre-defined class present in java.lang package. To get more info about String please type in the command prompt the following syntax:javap java.lang.String where lang is the package name and String is the class name.

Object Creation & Constructors in Java

As discussed earlier, objects are any real-time entity and a class is a template from which other objects are created. In other words, an object is created from a class. The new keyword is used to create objects. 

Every class possesses a constructor. A class can have more than one constructor. A parameterized constructor( a constructor that takes any number of arguments) may or may not be present in a class. But by default, a default constructor is always invoked when an object is created even if it is not defined explicitly in the class. In other words, if we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.

Note: Constructors must always have the same name as the class name. 

Syntax for Object Creation

Classname objectName = new constructorName();

Syntax for invoking Variables and Methods using Objects

objectname.variableName;       //To call variables

objectname.methodName();   // To call methods

Let us look at our 1st Example:

public class Test {
	public Test(){
		//this is a default constructor
		System.out.println("In default constructor");
	public Test(String name){
		//this is a constructor having parameter
		System.out.println("Value received in parameterized constructor is:"+name);
	public static void main(String[] args) {
		Test myObject = new Test(); //invoking the default constructor
		Test anotherObject = new Test("formulaeStack");//invoking the parameterized constructor


In our above example, we are creating two objects, 'myObject' and 'anotherObject'. The default constructor is invoked first followed by the parameterized constructor.

Coming to our 2nd Example:

public class Test {
	static int x=10;	//static variable
	int y=20; //non static variable


public class Demo {
public static void main(String[] args) {
	System.out.println(x); //compilation ERROR, cannot access directly
	System.out.println(Test.x);//accessing by class name
	Test obj=new Test();//new object creation
	System.out.println(obj.x);//access by object name
	System.out.println(obj.y);//access by obj name

In our above example, we have two classes Test and Demo. Static variables (we will come to static keyword and its uses later) can either be accessed by object name or by class name. However, it cannot be accessed directly.

Note: Static variables can be called directly but within the same class. It can also be called using a class name or object name in the same class as well as from outside class. Non-static variables can only be called using object name in the same as well as from outside class.

PrintStream - System.out.println()

  • It is used to display into the console.
  • Println is used to write into the standard output file, std_out file.
  • System is a predefined class.
  • Out is a pre-defined object.
  • The println( ) is a predefined method present in the core library.

How things work internally

command line argument

What is the difference between System.out and System.err

The difference between System.out and System.err is that the output can be redirected/stored into a file if it is generated by System.out however, if the output is generated by System.err then the output cannot be stored permanently.