This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Sep 27, 2007 8:54 AM by 3004 RSS

String[]args

807600 Newbie
Currently Being Moderated
i dont understand what is the usage of string [] args in the main method

how is it being used??

thanks

thanks
  • 1. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    It is used for command line arguments. The arguments are passed to the main() method through the String[] args
  • 2. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    main is a function designed so that it can be called from a command window also. The arguments that we pass while calling main function from command prompt becomes available as part of String args[] only.
    hope i make sense :-)
  • 3. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    thanks but, can u show me an example??

    sorry
  • 4. Re: String[]args
    3004 Newbie
    Currently Being Moderated
    The args parameter is just a way to pass startup data to your program. The way it's usually used is for handling command-line arguments:
    java MyClass file1 file2 file3
    
    
    public class MyClass {
      public static void main(String[] args) {
        for (String arg : args) {
          // arg will be "file1", then "file2", then "file3"
          // treat arg as a file name, process the file
        }
      }
    }
    But whatever "container" is running your app--e.g. an IDE like Eclipse, IntelliJ, or NetBeans--will also generally provide a way for you to provide arguments that it will then pass to main.
  • 5. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    class hello 
    {
        public static void main(String[] args) 
    {
            System.out.println(HOW TO DISPLAY ARGS ENTERED??);  
        }
    }
    how can i dispaly the args entered from the command line??
  • 6. Re: String[]args
    3004 Newbie
    Currently Being Moderated
    E.D.-inc wrote:
    class hello 
    {
    public static void main(String[] args) 
    {
    System.out.println(HOW TO DISPLAY ARGS ENTERED??);  
    }
    }
    how can i dispaly the args entered from the command line??
    See reply 4.

    If you can't adapt that to do what you want, then you need to start on page one of a Java tutorial.
  • 7. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    you dont have to be so LOUD.

    The arguments that you pass from the command promt come to program as a string array. So if you need them you have to iterate through the array.

    class hello
    {
    public static void main(String[] args)
    {
    for(int i=0;i<args.length;i++){       
    System.out.println(args);
    }
    }
    }
  • 8. Re: String[]args
    3004 Newbie
    Currently Being Moderated
    kelvin_nitk wrote:
    you dont have to be so LOUD.

    The arguments that you pass from the command promt come to program as a string array. So if you need them you have to iterate through the array.

    class hello
    {
    public static void main(String[] args)
    {
    for(int i=0;i<args.length;i++){       
    System.out.println(args);
    }
    }
    }

    1) You need to use [code] and [/code] tags in order to make your code readable.

    2) Enhanced for ( for (String arg : args) ) is preferred.
  • 9. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    first i made like
    class hello 
    {
        public static void main(String[] args) 
         {
    
         System.out.println(args);
         
        }
    }
    does compile but when i ran it from the command line like

    java hello helloworld

    or any other argument

    it is printed like

    Ljava.lang.string;@3e25a5

    it also happens wheni use the code suggested, the one that use loop

    any one can explain why??

    Edited by: E.D.-inc on Sep 28, 2007 12:19 AM
  • 10. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    oh
    ok thanks , it works, i changed the args to args[i] to print it..

    thnks
  • 11. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    lazy way:
    System.out.println(java.util.Arrays.toString(args));
  • 12. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    buddy you made me believe that i need to reconsider my java skills. Anyways hope your problem is solved now. Enjoyyyyy.....
  • 13. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    hahaha...even i looked at the code provided...
    my mistake was the sysout statement...
    i missed the part in that...
    neways...........
  • 14. Re: String[]args
    807600 Newbie
    Currently Being Moderated
    kelvin_nitk wrote:
    hahaha...even i looked at the code provided...
    my mistake was the sysout statement...
    i missed the i part in that...
    neways...........
    Note: for code to format correctly (for array index [i] not to turn into italics markup!) use code tags:
    [code]
    like = this;
    [/code]
1 2 Previous Next