Java Method Parameters and Arguments
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Method Parameters and Arguments

Jeevadharshan

Java Method Parameters and Arguments 

Java methods become truly powerful when they can accept input values. These inputs are called parameters, and the actual values passed during a method call are known as arguments. 
 
Understanding parameters allows you to create flexible, reusable, and dynamic methods. 

Parameters vs Arguments in Java

  • Parameter → variable defined in the method declaration 
  • Argument → actual value passed when calling the method
Think of parameters as placeholders and arguments as real data.

Declaring a Method with Parameters 

Parameters are written inside the parentheses of the method declaration: 
 
returnType methodName(dataType parameterName) 
 
Example 1: Single Parameter Method 
 
This method receives a student name and prints a welcome message. 
 
public class ParameterExample { 
 
    static void welcomeStudent(String name) { 
        System.out.println("Welcome, " + name + "!"); 
    } 
 
    public static void main(String[] args) { 
        welcomeStudent("Arun"); 
        welcomeStudent("Meena"); 
        welcomeStudent("Rahul"); 
    } 
}  

Output 

Welcome, Arun! 
Welcome, Meena! 
Welcome, Rahul!

Explanation 
  • name → parameter 
  • "Arun", "Meena", "Rahul" → arguments 

Multiple Parameters in a Method 

You can define multiple parameters separated by commas. The method call must supply the same number and order of arguments. 
 
Example 2: Method with Multiple Parameters 
 
public class MultiParameterExample { 
 
    static void displayEmployee(String empName, int experience) { 
        System.out.println(empName + " has " + experience + " years of experience."); 
    } 
 
    public static void main(String[] args) { 
        displayEmployee("Kiran", 3); 
        displayEmployee("Divya", 5); 
        displayEmployee("Sanjay", 8); 
    } 

Output 

Kiran has 3 years of experience. 
Divya has 5 years of experience. 
Sanjay has 8 years of experience. 

Parameter Order Matters 

Arguments must match the order and type of parameters.  

Correct: 

displayEmployee("Kiran", 3); 

Incorrect: 

displayEmployee(3, "Kiran"); // compilation error  

Using Conditional Logic Inside Methods  

Methods often include decision-making logic using if…else. 
 
Example 3: Access Control Check Method 
 
public class AccessCheckExample { 
 
    static void verifyAccess(int age) { 
 
        if (age < 18) { 
            System.out.println("Access denied: underage user."); 
        } else { 
            System.out.println("Access granted: welcome!"); 
        } 
    } 
 
    public static void main(String[] args) { 
        verifyAccess(16); 
        verifyAccess(21); 
    } 
}  

Output 

Access denied: underage user. 
Access granted: welcome!

Why Parameters Are Important 

Methods without parameters are limited. 

Parameters allow methods to: 
  • Accept dynamic input 
  • Work with different data 
  • Avoid hardcoding values 
  • Increase reusability 
  • Support real-world logic

Java Return Values  

In Java, methods can either perform an action or compute and return a result. When a method needs to send a value back to the caller, it uses a return type and the return keyword. 
 
Understanding return values is essential for building reusable logic, calculations, and real-world 
applications.

What Is a Return Value in Java? 

A return value is the result a method sends back after execution.
  • Methods with void → return nothing 
  • Methods with a data type → must return a value  

Declaring a Method That Returns a Value  

Replace void with the desired return type: 
 
static returnType methodName(parameters) { 
    return value; 
 
Example 1: Returning a Computed Value 
 
public class ReturnExample1 { 
 
    static int addBonus(int salary) { 
        return salary + 2000; 
    } 
 
    public static void main(String[] args) { 
        System.out.println(addBonus(15000)); 
    } 
}  

Output 

17000

Returning the Result of Two Parameters 

Methods often combine multiple inputs and return the result. 
 
Example 2: Returning a Product 
 
public class ReturnExample2 { 
 
    static int multiply(int a, int b) { 
        return a * b; 
    } 
 
    public static void main(String[] args) { 
        System.out.println(multiply(4, 6)); 
    } 
}  

Output  

24  

Storing Returned Values in Variables 

Storing results improves readability and maintainability. 
 
Example 3: Store and Use Return Value 
 
public class ReturnStoreExample { 
 
    static int calculateTotal(int price, int quantity) { 
        return price * quantity; 
    } 
 
    public static void main(String[] args) { 
 
        int totalCost = calculateTotal(250, 3); 
 
        System.out.println("Total cost: " + totalCost); 
    } 
}  

Practical Example: Using Return Values in a Loop  

Return values are commonly used in iterative processing. 

Example 4: Square Calculator 
 
public class ReturnLoopExample { 
 
    static int square(int number) { 
        return number * number; 
    } 
 
    public static void main(String[] args) { 
 
        for (int i = 1; i <= 5; i++) { 
            System.out.println("Square of " + i + " = " + square(i)); 
        } 
 
    } 
}  

Output 

Square of 1 = 1 
Square of 2 = 4 
Square of 3 = 9 
Square of 4 = 16 
Square of 5 = 25

Important Rules for Return Values

  • The method’s return type must match the returned value 
  • return immediately exits the method 
  • Methods with non-void type must return a value 
  • You can return variables, literals, or expressions
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !