Why does this code not work ?

Just started learning java.

Please let me know why the piece of code for String = NULL does not execute when I do not give any command line input. It does not print "No input detected".

import java.lang.*;

public class Echo{

public static void main (String S[]){

if (S!=null){
for (int i=0;i<S.length;i++){

System.out.println("No input detected");




  • Hey,
    A guess to the answer to your question is that the input argument string has been instantiated with a length of 0 (therefore it will not equal null). When there is no command line input, it remains that way.
    Thus to determine if any input was submitted on the command line is to use the test:
    [code]if (S.length>0) ...[/code]

    Regardes, se52
  • You are right, it is 0 and not NULL.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!