Return in void method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Return in void method

Is there void method that has return keyword? Because I saw one of them in a code related to the nodes of LinkedList. I want to know what's returned in this void method? It is sample: public void insertAfter(Node prev_node, int new_data) {     if (prev_node == null)     {         System.out.println("The given previous node cannot be null");         return;     } //It's part of a code!

13th Aug 2019, 6:35 PM
Amir01
Amir01 - avatar
3 Answers
+ 4
return is a reserved keyword to exit/end a method, with or without a value
13th Aug 2019, 6:53 PM
Brave Tea
Brave Tea - avatar
+ 4
No value is returned. Only the program control flow is returned to the point of (just after) the method invocation.
14th Aug 2019, 2:00 AM
Sonic
Sonic - avatar
+ 1
If the method returns void then return; just exits out of the method at that statement.
15th Aug 2019, 7:26 AM
Hassan Yousef
Hassan Yousef  - avatar