What is the impact of "void" and return in java? Why is "return" important to learn? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the impact of "void" and return in java? Why is "return" important to learn?

Java problem

10th Feb 2019, 3:38 PM
Niaz Makhdum
Niaz Makhdum - avatar
5 Answers
+ 2
Check out this code for a demonstration: https://code.sololearn.com/cx0d7DR4TIv0/?ref=app
10th Feb 2019, 4:38 PM
Lambda_Driver
Lambda_Driver - avatar
+ 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);   } }
10th Feb 2019, 7:25 PM
Danijel Ivanović
Danijel Ivanović - avatar
10th Feb 2019, 7:40 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
10th Feb 2019, 4:20 PM
Lambda_Driver
Lambda_Driver - avatar
+ 2
https://www.sololearn.com/post/44876/?ref=app
10th Feb 2019, 7:27 PM
Danijel Ivanović
Danijel Ivanović - avatar