Using int Instead Of String: public static void main (int[] args)

50,595

Solution 1

Everything passed into main method, the one used by the JVM to start a program, is a String, everything. It may look like the int 1, but it's really the String "1", and that's a big difference.

Now with your code, what happens if you try to run it? Sure it will compile just fine since it is valid Java, but your main method signature doesn't match the one required by the JVM as the starting point of a program.

For your code to run, you'd need to add a valid main method like,

public class IntArgsTest {
   public static void main(int[] args) {

      IntArgsTest iat = new IntArgsTest(args);

   }

   public IntArgsTest(int[] n) {
      System.out.println(n[0]);
   };

   public static void main(String[] args) {
      int[] intArgs = new int[args.length];

      for (int i : intArgs) {
         try {
            intArgs[i] = Integer.parseInt(args[i]);
         } catch (NumberFormatException e) {
            System.err.println("Failed trying to parse a non-numeric argument, " + args[i]);
         }
      }
      main(intArgs);
   }
}

And then pass some numbers in when the program is called.

Solution 2

Well, you can have any method with the name main with any number of arguments. But the JVM will look for the main method with the exact signature public static void main(String[]).

The main method you have defined is just another method to the class.

I don't have access to Windows now, but let me try it in a while. I did try on Fedora and of course I got the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: main

Note that the class would compile fine because of the above said reason.

Update: I tested on Windows 7 and the results are same. I'm surprised how you said it worked for you.

Solution 3

This code will not run actually. while the code compiles (because you don't need a main to compile), when you try to run it, you will get a "Main method not found" Error.

Even better, when I ran it it said

 "please define the main method as: public static void main(String[] args)

Solution 4

This code contains public static void main (int[] args) which does not works. Because the JVM takes the argument values as a string argument. It does not take any int argument. So if we want a int argument means we have to convert the string argument into integer argument. For running this code valid main method is required (ex:public static void main(String args[]))

Share:
50,595
Monarch Wadia
Author by

Monarch Wadia

Principal software developer at http://zeroprojects.ca. Happy to talk about all things web development. Feel free to message me. Ruby on Rails, Node.js, Tomcat Server, Linux & Mac, Java Jersey 2.x, REST, Nginx, API Creation, MySQL, Oracle SQL JavaScript, HTML5, CSS, Single page application / SPA Creation, Browserify, Jquery, Bootstrap 3, Gulp & Grunt, Angular.js, Bower, React.js + Flux, SASS Git, Agile Methodology, Github, Bitbucket, JIRA, Confluence, Advanced MS Excel, Advanced VBA & Macros, Microsoft Dynamics CRM

Updated on October 30, 2020

Comments

  • Monarch Wadia
    Monarch Wadia over 3 years

    I was under the impression that the main method had to have the form "public static void main (String[] args){}", that you couldn't pass int[] arguments.

    However, in windows commandline, when running the following .class file, it accepted both int and string as arguments.

    For example, using this command will give the output "stringers": "java IntArgsTest stringers"

    My question is, why? Why would this code accept a string as an argument without an error?

    Here is my code.

    public class IntArgsTest 
    {
        public static void main (int[] args)
        {
    
            IntArgsTest iat = new IntArgsTest(args);
    
        }
    
        public IntArgsTest(int[] n){ System.out.println(n[0]);};
    
    }
    
  • Monarch Wadia
    Monarch Wadia about 12 years
    Thank you for your reply. About your above-quoted error message: it gave me the same message when I tried running it on Eclipse. Did you try extracting a .class file and running it from command line? It seems to work fine.
  • Monarch Wadia
    Monarch Wadia about 12 years
    It worked for me when I extracted it to a .class file and ran it in command... it did not work for me in Eclipse, it gave me a similar message. Thank you for your reply!
  • Monarch Wadia
    Monarch Wadia about 12 years
    Very interesting way to do it! Thank you for the insight, I will be using this often I imagine. Doesn't answer my question though; it did run when I ran the .class file in cmd. Strange, no?
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels about 12 years
    @blackscholes: I can guarantee you that it did not run. You may have thought that it ran, but there is no way for your code to run in a standard Java compiler without additional code. Again, it will compile, but it will not run. Period.
  • Monarch Wadia
    Monarch Wadia about 12 years
    are you compiling to a .jar file? or a .class file? what i did was extract the .class file from the .jar file, place it on my desktop, and run it in cmd.... the file was "IntArgsTest.class". It is running for me.. this is why i'm confused! i could send you the file if you wish me to prove it.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels about 12 years
    @black: Don't do that. Write it out, compile it and run it in its pure state.
  • Monarch Wadia
    Monarch Wadia about 12 years
    @HFE: Ah okay, will try....... by the way, if I run the class file with the arguments, do you think the JVM doesn't require the main method? so i'm passing my args straight to the constructor of the IntArgsTest class?
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels about 12 years
    I have no idea what is in your class file. It's a black box.
  • Monarch Wadia
    Monarch Wadia about 12 years
    @HFE : Well, thank you for your help anyway, sir. I appreciate the time you've spent on this question!
  • Youssef G.
    Youssef G. about 12 years
    Yes, I ran it from command line (matter of fact, Eclipse won't see this as a Java application so you can't run it anyway unless you do your own application setup). There is no way this works, I suspect that you did NOT recompile between switching from String args[] to int args[].
  • Kevin Muhuri
    Kevin Muhuri almost 4 years
    That for loop format is used to access the value of elements in arrays and similar data structures. Only the value of index 0 will be correct and everything else in array intArgs will be 0. pastebin.com/Rd7SkGwJ