Java Method Return Types
🔹 Java Method Return Types
The return type of a method specifies what type of value the method will return to the caller. If a method does not return a value, we use void.
✅ 1. Syntax
-
returnType→ data type of the value returned (int,double,String, etc.) -
returnstatement → used to send a value back
✅ 2. Example 1: Method with int Return Type
📌 Output:
✅ 3. Example 2: Method with double Return Type
📌 Output:
✅ 4. Example 3: Method with String Return Type
📌 Output:
✅ 5. Example 4: Method with void Return Type
📌 Output:
❗ Note:
voidmethods do not return a value, so noreturnstatement with value is allowed.
✅ 6. Example 5: Using Method Return in Another Method
📌 Output:
🧠 Key Points
-
The return type defines what type of value a method gives back.
-
Use
voidif nothing is returned. -
A method can call another method and use its return value.
-
Always match the return type with the value being returned.
