Does the main method get an initialized array or are the strings in the command line enter directly in to the...











up vote
-2
down vote

favorite












For example, if I want to print an array length, I can not do that:



public class Test{
public static void main(String args){
System.out.println(worngParam({"first", "second", "ect"}));
}
public static int worngParam(String strings){
return strings.length;
}
}


This is an error!



The first two lines in main must be



String  strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));


But even so I can do that:



System.out.println(args.length);//If of course args is not empty


My question is how does the parameter get into the main method?



Although any method can accept a constant variabls such as 3, "word", 'a'. But she can not get an initialization of an array like this {1,8} or {"word2", "word3"}










share|improve this question




















  • 1




    You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
    – GriffeyDog
    Nov 7 at 21:53






  • 1




    { "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
    – MC Emperor
    Nov 7 at 21:58








  • 1




    The args string array is constructed by the JVM and passed to your main(String) method upon start.
    – MC Emperor
    Nov 7 at 22:03















up vote
-2
down vote

favorite












For example, if I want to print an array length, I can not do that:



public class Test{
public static void main(String args){
System.out.println(worngParam({"first", "second", "ect"}));
}
public static int worngParam(String strings){
return strings.length;
}
}


This is an error!



The first two lines in main must be



String  strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));


But even so I can do that:



System.out.println(args.length);//If of course args is not empty


My question is how does the parameter get into the main method?



Although any method can accept a constant variabls such as 3, "word", 'a'. But she can not get an initialization of an array like this {1,8} or {"word2", "word3"}










share|improve this question




















  • 1




    You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
    – GriffeyDog
    Nov 7 at 21:53






  • 1




    { "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
    – MC Emperor
    Nov 7 at 21:58








  • 1




    The args string array is constructed by the JVM and passed to your main(String) method upon start.
    – MC Emperor
    Nov 7 at 22:03













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











For example, if I want to print an array length, I can not do that:



public class Test{
public static void main(String args){
System.out.println(worngParam({"first", "second", "ect"}));
}
public static int worngParam(String strings){
return strings.length;
}
}


This is an error!



The first two lines in main must be



String  strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));


But even so I can do that:



System.out.println(args.length);//If of course args is not empty


My question is how does the parameter get into the main method?



Although any method can accept a constant variabls such as 3, "word", 'a'. But she can not get an initialization of an array like this {1,8} or {"word2", "word3"}










share|improve this question















For example, if I want to print an array length, I can not do that:



public class Test{
public static void main(String args){
System.out.println(worngParam({"first", "second", "ect"}));
}
public static int worngParam(String strings){
return strings.length;
}
}


This is an error!



The first two lines in main must be



String  strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));


But even so I can do that:



System.out.println(args.length);//If of course args is not empty


My question is how does the parameter get into the main method?



Although any method can accept a constant variabls such as 3, "word", 'a'. But she can not get an initialization of an array like this {1,8} or {"word2", "word3"}







java arrays parameters command-line-arguments main






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:43

























asked Nov 7 at 21:27









ploni almoni

32




32








  • 1




    You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
    – GriffeyDog
    Nov 7 at 21:53






  • 1




    { "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
    – MC Emperor
    Nov 7 at 21:58








  • 1




    The args string array is constructed by the JVM and passed to your main(String) method upon start.
    – MC Emperor
    Nov 7 at 22:03














  • 1




    You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
    – GriffeyDog
    Nov 7 at 21:53






  • 1




    { "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
    – MC Emperor
    Nov 7 at 21:58








  • 1




    The args string array is constructed by the JVM and passed to your main(String) method upon start.
    – MC Emperor
    Nov 7 at 22:03








1




1




You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
– GriffeyDog
Nov 7 at 21:53




You have to pass new String {"first", "second", "ect"} instead of {"first", "second", "ect"} when calling a method.
– GriffeyDog
Nov 7 at 21:53




1




1




{ "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
– MC Emperor
Nov 7 at 21:58






{ "first", "second" } is called an array initializer, and is shorthand for new String { "first", "second" }. The thing is, omitting the new String part is only allowed at the time of declaration of the array.
– MC Emperor
Nov 7 at 21:58






1




1




The args string array is constructed by the JVM and passed to your main(String) method upon start.
– MC Emperor
Nov 7 at 22:03




The args string array is constructed by the JVM and passed to your main(String) method upon start.
– MC Emperor
Nov 7 at 22:03












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Supposed you run your java class file like this



javac Test.java
java -cp . Test firstParam secondParam thirdParam


Then in your main method args will have a value like



args = new String{"firstParam", "secondParam", "thirdParam"};





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198087%2fdoes-the-main-method-get-an-initialized-array-or-are-the-strings-in-the-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Supposed you run your java class file like this



    javac Test.java
    java -cp . Test firstParam secondParam thirdParam


    Then in your main method args will have a value like



    args = new String{"firstParam", "secondParam", "thirdParam"};





    share|improve this answer



























      up vote
      0
      down vote



      accepted










      Supposed you run your java class file like this



      javac Test.java
      java -cp . Test firstParam secondParam thirdParam


      Then in your main method args will have a value like



      args = new String{"firstParam", "secondParam", "thirdParam"};





      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Supposed you run your java class file like this



        javac Test.java
        java -cp . Test firstParam secondParam thirdParam


        Then in your main method args will have a value like



        args = new String{"firstParam", "secondParam", "thirdParam"};





        share|improve this answer














        Supposed you run your java class file like this



        javac Test.java
        java -cp . Test firstParam secondParam thirdParam


        Then in your main method args will have a value like



        args = new String{"firstParam", "secondParam", "thirdParam"};






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 19:12

























        answered Nov 7 at 23:35









        Donat

        3566




        3566






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198087%2fdoes-the-main-method-get-an-initialized-array-or-are-the-strings-in-the-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ