0% found this document useful (0 votes)
2 views1 page

Java Methods: Void and Returning Examples

Uploaded by

denizbaba12211
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Java Methods: Void and Returning Examples

Uploaded by

denizbaba12211
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class MethodExample {

// --- Method 1: Void Method (Performs an Action) ---


/**
* Greets the user using their provided name.
* This is a 'void' method because it performs an action (printing)
* but does not return any data to the caller.
*
* @param name The name of the user to greet.
*/
public static void greetUser(String name) {
[Link]("Hello, " + name + "! Welcome to the Java Tutorial.");
}

// --- 📐 Method 2: Returning Method (Calculates and Returns Data) ---


/**
* Calculates the area of a rectangle.
* This method returns an 'int' (integer) which is the calculated area.
*
* @param length The length of the rectangle.
* @param width The width of the rectangle.
* @return The calculated area (length * width).
*/
public static int calculateRectangleArea(int length, int width) {
int area = length * width;
// The 'return' keyword sends the result back to the caller.
return area;
}

// --- 🚀 Main Method (Entry Point & Usage) ---


public static void main(String[] args) {

[Link]("--- Starting Method Calls ---");

// 1. Calling the 'void' method


// We just call it and it executes its action (printing to console).
greetUser("Alice");
greetUser("Bob");

// 2. Calling the 'returning' method


int len = 10;
int wid = 5;

// The method returns an 'int', so we must store the result in an 'int'


variable.
int resultArea = calculateRectangleArea(len, wid);

[Link]("\n--- Results ---");


[Link]("Rectangle Dimensions: Length=" + len + ", Width=" +
wid);
[Link]("The calculated area is: " + resultArea); // Output: 50
}
}

You might also like