SuperStar
SuperStar

Reputation: 495

Commandline args has no value, yet code gives an output?

I run this as a Java application in Eclipse. Surprisingly, it prints the first line. I was expecting it to print the second. I never entered anything in the command line, so why does this happen?

class Game
{   
    public static void main(String args[])
    {
        if(args != null){
            System.out.println("Lets do something with args");
        }else{
            System.out.println("Cant do something until args is not null"); 
        }
    }
}

EDIT -

Also tried another thing in response to one of the answers. Remove all code in main and substitute it with this -

System.out.println("Main method..."); 
for(int i = 0; i< args.length; i++){
System.out.println("i = " + i); 
} 

output is main method. Why ?

Upvotes: 3

Views: 5434

Answers (6)

ctn
ctn

Reputation: 2930

The args array is empty, not null. You need to do this:

if (args.length > 0) {
    System.out.println("Lets do something with args");
} else {
    System.out.println("Cant do something until args is not empty");
}

Before your program is run something like the following happens:

args = new String[0]; // create a new, empty array of strings

After this each argument from the command line, if any, is added to this array. Then your main function is called with this args array as the parameter.

There is a difference between a null array and and empty array. First of all, an array isn't just a set of values. It needs a container that stores 2 main things: the actual values and a length variable (so that you don't have to count the values each time). This container (a Java class) needs storage too. It needs to be allocated.

If you do:

String[] args;

you just have an args variable of type String[]. It has never been assigned to so it has no storage. Java initializes it to null. Think of null as the number zero. It is just a convention, meaning that it's not initialized. You can't access anything in args (args.length for example) because the class args should be naming doesn't exist yet. If you try you'll get a NullPointerException.

If you do:

String[] args = new String[0];

you now have an args variable of type String[], but it's also initialized with a new String[] class. This array class by default has no values and a length equal to zero. But this class exists. You can think of args as now being a number (address), different than zero (the null in the previous case), that identifies this class which is stored somewhere in memory. In effect, when you now do args.length you're telling Java to access the class that is identified by the address args contains (you normally can't see this address in the code, or anywhere, Java handles this for you), and from inside that class get the length variable. Similarly, when you do args[0] you first get the class from memory and then retrieve the first value from it.

Upvotes: 5

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

The command line String array is not null it is just empty. It does not contain any elements.

For your edit, the output is main method because the loop will not iterate one time also. This is because array has zero elements and hence it will not enter the loop a single time also.

Upvotes: 0

pinkpanther
pinkpanther

Reputation: 4798

The args is not null but is empty.

It's not that it is called by jvm like this

               `Game.main(null);`

But similar to this:

        `Game.main(new String[0]);` //in the case of no args.
  1. NULL:So if String[] args=null, you will get NullPointerException when you try to access like args.length etc. So, args is null here.

  2. EMPTY ARRAY:But if String[] args=new String[0], you won't run into such problems.

So, you can just see if the array has no elements in which case args.length is zero. So, args is empty here.

  `if(args.length>0)` is what you need.

Hope this helps.

EDIT:

output is main method. Why ?

Because args.length is 0 in your case and i=0, i<args.length,0<0 condition fails hence no iteration is made.

Upvotes: 1

arshajii
arshajii

Reputation: 129497

If you don't pass any command line arguments to the program, args will not be null but rather a 0-length String[]. Try printing args.length - a null value would throw an exception whereas a 0-length array would print 0.

Upvotes: 2

fmodos
fmodos

Reputation: 4568

The array is empty, if you want to test with arguments in eclipse follow the steps below:

Right click the class -> Run as -> Run Configuration -> Arguments Tab

Add some text to the Program Arguments text area

Upvotes: 0

zw324
zw324

Reputation: 27180

The command line args is just empty [], it is not null.

Upvotes: 0

Related Questions