Java: What exactly does main(String args[]) actually mean?

Discussion in 'Software' started by Pandemonium Syndicate, May 4, 2008.

  1. Pandemonium Syndicate

    Pandemonium Syndicate Private E-2

    Hey, Ima Noob here, but I see others have posted assignment problems, and I have run into my own, and I believe it stems from a fundamental ignorance concerning EXACTLY what happens when main() is called. We all know that
    public void main(String args[]) is the starting point of any Java program, as well as many other languages, but exactly does it mean? why is it vital? how does it work? When I actually break it down this is what I get:

    a public void method, so it is available to everyone, and has no return value,
    It takes in a String array, named args[]

    and that's...it. My problem stems from having a given main() in our program actually USE args[], it is literally something like:

    public static void main(String[] args) {
    int n = 0;
    if (args[n].equals("-t")) {
    setTrace(true);
    ++n;
    }
    load(args[n]);
    run();
    }

    so what we got obviously is a conditional that executes only if at
    args[0] == -t, it turns on trace, which in the rest of the program is essentially some non important debug output. But the interesting part is that
    load() is the method that I call to read a given file, which it does not pick up. So my question is this, what exactly does args[] MEAN? why does it freak out when we try to work with it in general? I understand that this code snippet does little to nothing, but what I am asking is for a quick explanation of what happens when main is called in terms of actual execution. Thanks for reading.
     
  2. PC-XT

    PC-XT Master Sergeant

    I've had trouble with command-line parameters in other languages, also. Sometimes, it's an error in my program, other times, I've used a special character in the command line, which messed things up. I once used / to start switches, but that didn't work. I don't think - would do that, but don't know for sure. As far as I know, Java takes the command line, and interprets the stuff it needs to configure itself and get the class, then divides the arguments into an array of Strings, which it passes to main.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds