Parameters and Return Types in Methods
In this lesson, we take a closer look at the parameters and return types of methods in Java.
We'll cover the following
Parameters
The parameters are the inputs that a Java method takes. These are given as an ordered list in parentheses next to the name of the method. We can have any number of parameters.
-
If a method does not have any parameters, the parentheses are left empty.
public static void function() { // implementation here }
-
Otherwise, for each parameter, we give the type and the name, separated by a comma, in the method’s signature.
public static void function(type p1, type p2, ..., type pN) { // implementation here }
Here, the type
of the parameters can be primitive (e.g., int
or double
) or reference (e.g., a class’s object).
Return type
The return type of a method tells you what kind of value that method returns. For example, a method that calculates the area of a quadrilateral might have a double
return type (lines 4-8), whereas a method that concatenates day, month, and year to return a date may have a String
return type (lines 11-15).
Get hands-on with 1200+ tech skills courses.