-4

I am confused on why main method on java has to be:

public static void main(String[] args)

My code is


public class MergeSorted 
{
    public static void main (int[] nums1, int m, int[] nums2, int n) 
    {
       ----
    }
}

Will executing >>java MergeSorted [2,4,5,0,0,0],3,[1,3],2; help?

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    The code is fine. Do you actually submit any inputs to the script? – go2nirvana Feb 15 '21 at 11:14
  • 3
    Does this answer your question? [Can't send input to running program in Sublime Text](https://stackoverflow.com/questions/19254765/cant-send-input-to-running-program-in-sublime-text) – go2nirvana Feb 15 '21 at 11:21
  • @Trusha_Patel Please ask a new question if you have one, instead of changing the question. – MC Emperor Mar 12 '21 at 12:41

2 Answers2

3

In Java the main method gets noticed by these exact method signature. The String[] args are the arguments the program started with. So when you compile your program, goto the command line execute your program you can put parameters behind the "start command"

For example

java -jar x.jar --myflag
                ^ args...

Then your String array is going to contain "--myflag", you can process those arguments to get the information that you want from it..

1

You have to have it as you will run the program as a command line application. It it is javax application would be requiring a main method.

MergeSort.java

public class MergeSort {
   public static void main(String[] args) {
      // get input via input stream
      mergeSort(...); // call method
   }

   public static void mergeSort(int[] nums1, int m, int[] nums2, int n) {
      // logic here
   }
}
Naveen Vignesh
  • 1,351
  • 10
  • 21