Skip to main content

Method Parameters and Return Values

Method Example​

Here are a few examples to illustrate different types of methods:

Method with No Parameters and No Return Value​

public void printHello() {
System.out.println("Hello, World!");
}

Method with Parameters and a Return Value​

public int multiply(int x, int y) {
return x * y;
}

Method with No Parameters but a Return Value​

public String getGreeting() {
return "Hello, Java!";
}

Method with Multiple Parameters​

public double calculateArea(double width, double height) {
return width * height;
}

Calling Methods​

To call a method, use the method name followed by parentheses. If the method requires parameters, provide the appropriate arguments within the parentheses.

Example​

public class Main {
public static void main(String[] args) {
Main obj = new Main();

// Calling a method with no parameters and no return value
obj.printHello();

// Calling a method with parameters and a return value
int result = obj.add(5, 3);
System.out.println("Sum: " + result);

// Calling a method with no parameters but a return value
String greeting = obj.getGreeting();
System.out.println(greeting);

// Calling a method with multiple parameters
double area = obj.calculateArea(5.5, 4.0);
System.out.println("Area: " + area);
}

public void printHello() {
System.out.println("Hello, World!");
}

public int add(int a, int b) {
return a + b;
}

public String getGreeting() {
return "Hello, Java!";
}

public double calculateArea(double width, double height) {
return width * height;
}
}