I have a question about a function of return in Java
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
add a comment |
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
add a comment |
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
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
java return output result
edited Nov 10 at 15:50
Hülya
456119
456119
asked Nov 10 at 14:33
YsXii
51
51
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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.
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 10 at 14:38
nullpointer
41.3k1087175
41.3k1087175
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 15:46
Rishabh Agarwal
772318
772318
answered Nov 10 at 14:44
Falm
11
11
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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