I have a question about a function of return in Java












-3














class MethodDemo6 {
public static String numbering(int init, int limit) {
int i = init;
String output = "";
while (i < limit) {
output += i;
i++;
}
return output;
}

public static void main(String args) {
String result = numbering(1, 5);
System.out.println(result);
}


The part that I have been wondering is, the code 'returned' output.



After that, when the main part starts, it says String result = numbering(1, 5);



Instead of String output = numbering(1, 5);



It works, But I still cannot understand the mechanism.



Are result and output the same thing ?



Or are both like reserved words?










share|improve this question





























    -3














    class MethodDemo6 {
    public static String numbering(int init, int limit) {
    int i = init;
    String output = "";
    while (i < limit) {
    output += i;
    i++;
    }
    return output;
    }

    public static void main(String args) {
    String result = numbering(1, 5);
    System.out.println(result);
    }


    The part that I have been wondering is, the code 'returned' output.



    After that, when the main part starts, it says String result = numbering(1, 5);



    Instead of String output = numbering(1, 5);



    It works, But I still cannot understand the mechanism.



    Are result and output the same thing ?



    Or are both like reserved words?










    share|improve this question



























      -3












      -3








      -3







      class MethodDemo6 {
      public static String numbering(int init, int limit) {
      int i = init;
      String output = "";
      while (i < limit) {
      output += i;
      i++;
      }
      return output;
      }

      public static void main(String args) {
      String result = numbering(1, 5);
      System.out.println(result);
      }


      The part that I have been wondering is, the code 'returned' output.



      After that, when the main part starts, it says String result = numbering(1, 5);



      Instead of String output = numbering(1, 5);



      It works, But I still cannot understand the mechanism.



      Are result and output the same thing ?



      Or are both like reserved words?










      share|improve this question















      class MethodDemo6 {
      public static String numbering(int init, int limit) {
      int i = init;
      String output = "";
      while (i < limit) {
      output += i;
      i++;
      }
      return output;
      }

      public static void main(String args) {
      String result = numbering(1, 5);
      System.out.println(result);
      }


      The part that I have been wondering is, the code 'returned' output.



      After that, when the main part starts, it says String result = numbering(1, 5);



      Instead of String output = numbering(1, 5);



      It works, But I still cannot understand the mechanism.



      Are result and output the same thing ?



      Or are both like reserved words?







      java return output result






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 15:50









      Hülya

      456119




      456119










      asked Nov 10 at 14:33









      YsXii

      51




      51
























          3 Answers
          3






          active

          oldest

          votes


















          0














          The output named variable from inside the method is only visible inside the method (it is called a "local" variable).



          This means that it is used only inside that block of code for computation. The return statement from the final line of the method brings the result outside of it, so when you run String result = numbering(1, 5);, the result variable will have only the resulted information from the method.



          For more information about methods, you can refer to this link






          share|improve this answer





















          • Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
            – YsXii
            Nov 10 at 14:41












          • The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
            – Tiberiu Zulean
            Nov 10 at 14:45



















          0














          The variable output of type String is local to the method numbering.



          On the other hand, this method returns a String which is stored in the variable named result within your main method.






          share|improve this answer





























            0














            When calling a function, if the function returns anything the value is returned to the caller.



            Your function numbering is of type String because it’s signature is written as



            public static ‘String’ numbering (int...)


            This means it returns a String to the caller, which in your case is result and the String that it returns is assigned to the variable output.






            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',
              autoActivateHeartbeat: false,
              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%2f53239970%2fi-have-a-question-about-a-function-of-return-in-java%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              The output named variable from inside the method is only visible inside the method (it is called a "local" variable).



              This means that it is used only inside that block of code for computation. The return statement from the final line of the method brings the result outside of it, so when you run String result = numbering(1, 5);, the result variable will have only the resulted information from the method.



              For more information about methods, you can refer to this link






              share|improve this answer





















              • Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
                – YsXii
                Nov 10 at 14:41












              • The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
                – Tiberiu Zulean
                Nov 10 at 14:45
















              0














              The output named variable from inside the method is only visible inside the method (it is called a "local" variable).



              This means that it is used only inside that block of code for computation. The return statement from the final line of the method brings the result outside of it, so when you run String result = numbering(1, 5);, the result variable will have only the resulted information from the method.



              For more information about methods, you can refer to this link






              share|improve this answer





















              • Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
                – YsXii
                Nov 10 at 14:41












              • The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
                – Tiberiu Zulean
                Nov 10 at 14:45














              0












              0








              0






              The output named variable from inside the method is only visible inside the method (it is called a "local" variable).



              This means that it is used only inside that block of code for computation. The return statement from the final line of the method brings the result outside of it, so when you run String result = numbering(1, 5);, the result variable will have only the resulted information from the method.



              For more information about methods, you can refer to this link






              share|improve this answer












              The output named variable from inside the method is only visible inside the method (it is called a "local" variable).



              This means that it is used only inside that block of code for computation. The return statement from the final line of the method brings the result outside of it, so when you run String result = numbering(1, 5);, the result variable will have only the resulted information from the method.



              For more information about methods, you can refer to this link







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 at 14:37









              Tiberiu Zulean

              14713




              14713












              • Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
                – YsXii
                Nov 10 at 14:41












              • The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
                – Tiberiu Zulean
                Nov 10 at 14:45


















              • Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
                – YsXii
                Nov 10 at 14:41












              • The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
                – Tiberiu Zulean
                Nov 10 at 14:45
















              Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
              – YsXii
              Nov 10 at 14:41






              Thanks for the explanation, So, if I understood correctly, from this part 'String result = numbering(1, 5);', numbering(1, 5) directly indicates output. am I right ?
              – YsXii
              Nov 10 at 14:41














              The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
              – Tiberiu Zulean
              Nov 10 at 14:45




              The result variable (which is of String data type, meaning it can hold a sequence of characters as information) is assigned with the output from the method named numbering with parameters 1 and 5. In short, result has the output.
              – Tiberiu Zulean
              Nov 10 at 14:45













              0














              The variable output of type String is local to the method numbering.



              On the other hand, this method returns a String which is stored in the variable named result within your main method.






              share|improve this answer


























                0














                The variable output of type String is local to the method numbering.



                On the other hand, this method returns a String which is stored in the variable named result within your main method.






                share|improve this answer
























                  0












                  0








                  0






                  The variable output of type String is local to the method numbering.



                  On the other hand, this method returns a String which is stored in the variable named result within your main method.






                  share|improve this answer












                  The variable output of type String is local to the method numbering.



                  On the other hand, this method returns a String which is stored in the variable named result within your main method.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 14:38









                  nullpointer

                  41.3k1087175




                  41.3k1087175























                      0














                      When calling a function, if the function returns anything the value is returned to the caller.



                      Your function numbering is of type String because it’s signature is written as



                      public static ‘String’ numbering (int...)


                      This means it returns a String to the caller, which in your case is result and the String that it returns is assigned to the variable output.






                      share|improve this answer




























                        0














                        When calling a function, if the function returns anything the value is returned to the caller.



                        Your function numbering is of type String because it’s signature is written as



                        public static ‘String’ numbering (int...)


                        This means it returns a String to the caller, which in your case is result and the String that it returns is assigned to the variable output.






                        share|improve this answer


























                          0












                          0








                          0






                          When calling a function, if the function returns anything the value is returned to the caller.



                          Your function numbering is of type String because it’s signature is written as



                          public static ‘String’ numbering (int...)


                          This means it returns a String to the caller, which in your case is result and the String that it returns is assigned to the variable output.






                          share|improve this answer














                          When calling a function, if the function returns anything the value is returned to the caller.



                          Your function numbering is of type String because it’s signature is written as



                          public static ‘String’ numbering (int...)


                          This means it returns a String to the caller, which in your case is result and the String that it returns is assigned to the variable output.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 10 at 15:46









                          Rishabh Agarwal

                          772318




                          772318










                          answered Nov 10 at 14:44









                          Falm

                          11




                          11






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239970%2fi-have-a-question-about-a-function-of-return-in-java%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

                              Schultheiß

                              Liste der Kulturdenkmale in Wilsdruff

                              Android Play Services Check