I have a question about a function of return in Java











up vote
-3
down vote

favorite












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




























    up vote
    -3
    down vote

    favorite












    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


























      up vote
      -3
      down vote

      favorite









      up vote
      -3
      down vote

      favorite











      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

      42719




      42719










      asked Nov 10 at 14:33









      YsXii

      51




      51
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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


















          up vote
          0
          down vote













          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




























            up vote
            0
            down vote













            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










            New contributor




            Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















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

              Post as a guest
































              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              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















              up vote
              0
              down vote



              accepted










              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













              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              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

              14513




              14513












              • 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












              up vote
              0
              down vote













              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

























                up vote
                0
                down vote













                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























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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

                  34.1k1069138




                  34.1k1069138






















                      up vote
                      0
                      down vote













                      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










                      New contributor




                      Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






















                        up vote
                        0
                        down vote













                        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










                        New contributor




                        Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.




















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          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










                          New contributor




                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          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










                          New contributor




                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer








                          edited Nov 10 at 15:46









                          rishabh agarwal

                          679315




                          679315






                          New contributor




                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered Nov 10 at 14:44









                          Falm

                          11




                          11




                          New contributor




                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Falm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                               

                              draft saved


                              draft discarded



















































                               


                              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




















































































                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python