Home » Java » java basic question

java basic question

Posted by: admin December 28, 2021 Leave a comment

Questions:
public static void main( String arg[] )

In the above statement, can I use an int array in place of the String array? What happens if I don’t put anything in the parenthesis, i.e if I use an empty parenthesis?

Answers:

When you compile the code with the changes that you mentioned, it will compile successfully. When you try to run, JVM checks for the main method with String array as arguments. Since there is no main method with String array as argument, your code will not execute successfully and it throws NoSuchMethodError.

###

No, I think you can’t use int array instead of String array.because the argument int is used by the operating system to pass an integer value specifying the number of command-line arguments entered by the user. so you must follow the following pattern.

public static void main(String[] args)

public static void main(String args[]) 

###

I’m not sure what you mean by Int array but no, you cannot. The method signature needs to exactly match public static void main(String[] args). The only thing you can change is the name of the argument. The name of the method, the argument type, the visibility (public vs private, etc) is what the runtime uses to find the method itself. If it does not conform to that signature, it is not an entry point method and consequently will not be called when your application starts up.

However, it should be noted that what you’re suggesting will compile without issue. The problems will not arise until you attempt to run the application.

###

The code will compile but not run.

The reason for the string[] is so that people can pass parameters through the command line.

###

The compiler will accept but the runtime will give you a NoSuchMethodError

###

If you’re looking to get numbers instead of strings, use this approach

public static void main(String[] args) {

    int[] numbers = new int[args.length];
    for(int i = 0; i < args.length; i++) numbers[i] = Integer.parseInt(args[i]);

}

Handling exceptions is exercise for the reader.

Other answers explain quite well your other issues of the ‘why’.

###

The main method is the entry point for the java application. If you look at the java language specification, it specifically states:

The method main must be declared
public, static, and void. It must
accept a single argument that is an
array of strings. This method can be
declared as either

public static void main(String[] args)

or

public static void main(String... args)

The String[] array passed in contains any command line arguments that have been passed in by whatever launched the application. You can then read them and convert them as you need.

###

No,it produces a runtime error like
Main method not found in class test, please define the main method as:
public static void main(String[] args)

JAVA is more specific about main method signature, It considers your method as a general method in a class.

###

If you want to convert String args from main static method to int then do the following:

public class StringToIntArgs {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] anIntArray = new int[args.length];

        for(int i = 0; i < args.length; ++i) {
            anIntArray[i] = Integer.valueOf(args[i]);

            System.out.println(((Object)anIntArray[i]).getClass().getName());
        }
    }

}

Now compile and run the java code with arguments like below:

C:\Users\smith\Documents\programming\java>javac StringToIntArgs.java

C:\Users\smith\Documents\programming\java>java StringToIntArgs 1 2 3 4 5
java.lang.Integer
java.lang.Integer
java.lang.Integer
java.lang.Integer
java.lang.Integer