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.)

  • return statement → used to send a value back


2. Example 1: Method with int Return Type


 

📌 Output:

Sum = 15

3. Example 2: Method with double Return Type


 

📌 Output:

Product = 7.0

4. Example 3: Method with String Return Type


 

📌 Output:

Hello, Alice!

5. Example 4: Method with void Return Type


 

📌 Output:

Welcome to Java!

Note: void methods do not return a value, so no return statement with value is allowed.


6. Example 5: Using Method Return in Another Method


 

📌 Output:

Sum of squares = 25

🧠 Key Points

  • The return type defines what type of value a method gives back.

  • Use void if nothing is returned.

  • A method can call another method and use its return value.

  • Always match the return type with the value being returned.

You may also like...