0
What is the impact of "void" and return in java? Why is "return" important to learn?
Java problem
5 Respuestas
+ 2
Check out this code for a demonstration:
https://code.sololearn.com/cx0d7DR4TIv0/?ref=app
+ 4
In JAVA, 'void' is used when you do not want your method to return anything.
In Java, while defining a method, you usually define following three things
with it :
1. Access specifiers such as public, protected.
2. Return type of the method.
3. Parameters to be passed in the method and their types.
The return type of a method tells what kind of value a method is supposed to return to the point from where it is called.
However, if you use 'void' as the return type of a method, it doesn't need to return anything.
It just performs the work written through the code, and returns the control back to point from where it was called.
For example:
Return Value from Method
class Square {
public static int square() {
// return statement
return 10 * 10;
}
public static void main(String[] args) {
int result;
result = square();
System.out.println(
"Squared value of 10 is: "+ result);
}
}
+ 4
Please,
Remember to use the search bar to avoid from posting duplicate threads!
https://www.sololearn.com/Discuss/120043/?ref=app
https://www.sololearn.com/Discuss/990239/?ref=app
https://www.sololearn.com/Discuss/271102/?ref=app
+ 2
If you want to reuse the data from the method elsewhere in the program, then a method with a return value is important. Make sure that you have a variable that catches the return value from the method.
If you use void in your method, whatever happens within the method (e.g. calculations, data manipulation) is not saved and is automatically deleted after the method is finished running.
Read the comments on the lesson. They are enlightening.
https://www.sololearn.com/learn/Java/2153/?ref=app
+ 2
https://www.sololearn.com/post/44876/?ref=app