Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Main methods with different signatures

Tags:

java

main

I have following class.

public class Test {

    public static void main(Integer[] args) {
        System.out.println("This is not a main"); 
    }   

    public static void main(String[] args) {
        System.out.println("This is the main"); 
    }
}

In here there are two main method which are accept Integer[] and String [] as input argument. My question is how JVM always load second method as main method of this class. Why always consider input argument as array of String?

like image 555
Ruchira Gayan Ranaweera Avatar asked Aug 12 '13 19:08

Ruchira Gayan Ranaweera


2 Answers

Because that's what Java always looks for. Java Language Specification, Section 12.1.4:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String

like image 144
rgettman Avatar answered Nov 02 '22 00:11

rgettman


Because Strings are what you're passing into the command line.

the 45 from

myProgram.exe 45

is not an integer. it is a string containing the characters 4 and 5

It just so happens that you can use a string like "45" to represent an integer. It's a little more difficult to do it the other way around.(for the user at least)

like image 41
Sam I am says Reinstate Monica Avatar answered Nov 02 '22 01:11

Sam I am says Reinstate Monica