Java Return Values

Getting results from methods

📤 What are Return Values?

Return values allow methods to send data back to the caller. Instead of just performing actions, methods can calculate results and return them for use elsewhere.


// Method that returns a value
public static int addNumbers(int a, int b) {
    return a + b;
}
                                    

Usage: int result = addNumbers(5, 3);

result = 8

Key Return Value Concepts

🔄

Return Type

Specify what type of data to return

public static int getValue() {}
↩️

Return Statement

Use return keyword to send data back

return 42;
📦

Storing Results

Capture returned values in variables

int result = getValue();
🚫

Void Methods

Methods that don't return anything

public static void print() {}

🔹 Basic Return Examples

Methods that return different types of values:

public class ReturnExample {
    
    // Returns an integer
    public static int getAge() {
        return 25;
    }
    
    // Returns a string
    public static String getName() {
        return "Alice";
    }
    
    // Returns a boolean
    public static boolean isStudent() {
        return true;
    }
    
    public static void main(String[] args) {
        int age = getAge();
        String name = getName();
        boolean student = isStudent();
        
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Is Student: " + student);
    }
}

Output:

Name: Alice

Age: 25

Is Student: true

🔹 Calculation Methods

Methods that perform calculations and return results:

public class Calculator {
    
    // Add two numbers
    public static int add(int a, int b) {
        return a + b;
    }
    
    // Multiply two numbers
    public static int multiply(int x, int y) {
        return x * y;
    }
    
    // Calculate area of rectangle
    public static double calculateArea(double length, double width) {
        return length * width;
    }
    
    public static void main(String[] args) {
        int sum = add(10, 5);
        int product = multiply(4, 7);
        double area = calculateArea(5.5, 3.2);
        
        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
        System.out.println("Area: " + area);
    }
}

Output:

Sum: 15

Product: 28

Area: 17.6

🔹 Using Return Values

Different ways to use returned values:

public class ReturnUsage {
    
    public static int square(int number) {
        return number * number;
    }
    
    public static String createGreeting(String name) {
        return "Hello, " + name + "!";
    }
    
    public static void main(String[] args) {
        // Store in variable
        int result = square(5);
        System.out.println("Square of 5: " + result);
        
        // Use directly in print
        System.out.println("Square of 3: " + square(3));
        
        // Use in calculations
        int total = square(4) + square(6);
        System.out.println("4² + 6² = " + total);
        
        // Store string result
        String greeting = createGreeting("Bob");
        System.out.println(greeting);
    }
}

Output:

Square of 5: 25

Square of 3: 9

4² + 6² = 52

Hello, Bob!

🧠 Test Your Knowledge

What keyword is used to send a value back from a method?