Results 1 to 8 of 8
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Smile What does String args[] stand for?

    I find myself always including em in all my main functions just to avoid any un known error.
    But what does String args[] stand for?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This allows one to pass argument strings to your program when you start it. For instance if you have a class called MyProgram.java with a main method, and you then compile it to MyProgram.class, and finally if you run this like so:
    Java Code:
    java MyProgram foo bar
    The two strings foo and bar are passed to the main method within the args array in positions args[0] and args[1].

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    ahh So I can wright java myapp hello
    And the funtion is:
    Java Code:
    class test{
    public static void main(String args[]){
    System.out.println(args[0]);
    }
    }
    can I?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    sort of, for that since the class is called test, you'd have to call
    Java Code:
    java test hello

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you'd also need to make sure that args[0] exists. you might have args of size 0. unlike c, the program name is NOT the first arg

  6. #6
    CaseyRenee87 is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default

    Interesting! so that's what it's used for.

    I was wondering what the String args[] purpose was as well, and why it was needed in every main method.

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    But it is not needed unless ur gona put in some input! Right?
    And can I do (int args[]) and execut by typing:
    test 54

    ?

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. Replies: 6
    Last Post: 05-19-2009, 12:22 PM
  3. String[] arg and String... args
    By mahendra.athneria in forum Advanced Java
    Replies: 4
    Last Post: 04-28-2009, 07:20 PM
  4. Replies: 9
    Last Post: 11-22-2008, 11:03 PM
  5. [SOLVED] ReadLine(String fmt,Object... args) of Console class
    By Pooja Deshpande in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 05:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •