Advertisements
Advertisements
Question
Differentiate between methods with return type void and int.
Distinguish Between
Advertisements
Solution
| Feature | Method with void Return Type | Method with int Return Type |
| Core Meaning | The method performs an action but does not send back any data to the caller. | The method performs a calculation and must send back a whole number to the caller. |
| Return Keyword Syntax | Keyword Syntax The return; statement is optional and is only used to exit the method early. | A statement is mandatory at the end of the method execution paths. |
| Variable Assignment | Its output cannot be assigned to any variable (e.g., int x = obj.printMessage(); causes a compile error). | Its output can be captured and saved directly into an integer variable (e.g., int result = obj.calculateSum();). |
| Common Use Case | Used for printing information, updating states, or clearing data (e.g., System.out.println()). | Used for mathematical operations, counting elements, or getting array sizes (e.g., Math.abs()). |
shaalaa.com
Is there an error in this question or solution?
Chapter 3: User-Defined Methods - Exercises [Page 91]
