What are Method Parameters in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Parameters in Java refer to variables that can be passed to methods or constructors, capable of storing diverse data types like byte, string, int, float, and more. Methods/constructors can take a single parameter or multiple parameters in Java. Values (arguments) can be passed to these parameters during method calls, maintaining the exact count and order. Java's latest feature allows accessing formal parameter names of methods or constructors via the java.lang.reflect package, utilizing classes like Method and Parameter.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Parameters and Arguments in Java

Parameters in Java are variables declared in a method's signature, specifying the data type and identifier the method expects to receive when it's called. Parameters in Java act as placeholders within the method, representing values that will be supplied during the method call.

Syntax:

where, returnType: The type of value the method will return (use void if the method doesn't return anything). methodName: The name of the method. dataType: The data type of the parameter. parameterName1, parameterName2, ...: Names of the parameters the method expects.

Arguments

These are the actual values or expressions that are passed to a method when it's invoked. These values correspond to the parameters declared in the method's signature.

Syntax:

where,

methodName: The name of the method you want to call. argumentValue1, argumentValue2, ...: Actual values or expressions that you are passing to the method as arguments.

Sharpen Your Fundamentals with Free Learning

Example

Here is a clear distinction between arguments and parameters in Java with a code example.

Code:

Output:

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Parameter Vs Argument

Here is a tabular comparison between arguments and parameters in Java.

AspectParametersArguments
DefinitionVariables declared in method/constructorActual values/expression passed during a method call
LocationMethod/constructor declarationMethod call
UsageUsed within the method's bodyPassed from the calling code to the method
RolePlaceholder for dataActual data to be processed by the method
Exampleint add(int x, int y)add(5, 3)

Multiple Parameters in Java

Methods/contructors can accept more than one parameter in Java. Here is an example to demonstrate how to define and use a method with multiple parameters in Java:

Code:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Return Values

In Java, return values are the results that a method can send back to the code that called it. When a method is executed and has a value to return, it uses the return keyword to pass that value back to the caller. Return values are essential for obtaining the outcome of computations or operations performed within a method.

Consider the following code snippet.

Code:

Output:

Explanation:

In the example above:

  1. The square() method returns an integer value, which is the squared result of the input number.
  2. The calculateCircleArea() method returns a double value, which is the calculated area of a circle given its radius.
  3. The isPositive() method returns a boolean value indicating whether the input number is positive.

The void return type in Java indicates that a method does not return any value. Instead, methods with a void return type are typically used to perform actions, operations, or tasks without producing a result that needs to be used or stored.

Using If…Else within a Method

Using if...else statements within a method in Java allows you to make decisions based on certain conditions. These conditions determine which block of code gets executed. Here's an example to illustrate the concept.

Code:

Output:

Explanation:

In the example above:

the method take a integer and check whether it is positive or negative using the if else condition.

FAQs

Q. What are method parameters in Java?

A. Method parameters in Java are variables defined within a method's parentheses that allow values to be passed from the calling code to the method for processing.

Q. Can methods have multiple parameters in Java?

A. Yes, methods can have multiple parameters in Java, enabling them to accept and process multiple values passed during method calls.

Q. Can Java methods return values based on parameters?

A. Yes, methods can analyze parameter values and use them to compute results that are returned to the caller using the return keyword.

Conclusion

  • Parameters in Java refer to variables declared in the method/constructor while arguments refer to actual values/expressions passed during a method call.
  • Methods can return values using the return statement, helping to convey the outcomes of computations or operations to the calling code.
  • Methods can have multiple parameters in Java, enabling them to accept and process multiple values passed during method calls.
  • Utilizing if...else statements within methods allows for condition-based execution, enabling dynamic responses based on input conditions.
  • Method parameters in Java serve as channels for information exchange between different program parts, fostering organized and efficient communication.