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?
java return output result
add a comment |
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?
java return output result
add a comment |
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?
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
42719
42719
asked Nov 10 at 14:33
YsXii
51
51
add a comment |
add a comment |
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
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 |
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.
add a comment |
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.
New contributor
add a comment |
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
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 |
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
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 |
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
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
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
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 |
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.
add a comment |
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.
add a comment |
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.
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
34.1k1069138
34.1k1069138
add a comment |
add a comment |
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.
New contributor
add a comment |
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.
New contributor
add a comment |
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.
New contributor
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.
New contributor
edited Nov 10 at 15:46
rishabh agarwal
679315
679315
New contributor
answered Nov 10 at 14:44
Falm
11
11
New contributor
New contributor
add a comment |
add a comment |
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
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
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
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
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