-
Notifications
You must be signed in to change notification settings - Fork 0
Methods
Büşra Oğuzoğlu edited this page Jul 6, 2022
·
13 revisions
A method in Java is a collection of statements that are grouped together to perform an operation, it has inputs (can be multiple) and a single output (the return value).
Structure of a method is as follows:
private static double findAverage(double[] numbers) {
// Method Body
double sum = 0.0;
for (double e : numbers)
sum += e;
return sum / numbers.length;
}-
private static double findAverage(double[] numbers)is the method header. -
double is the return type.
-
findAverageis the method name. -
double[] numbersare the method parameters. -
Methods may have a return statement, they do not need to have it if their return type is void.
private static int max(int a, int b) {
int result = a;
if (b > a)
result = b;
return result;
}int input1 = 5;
int input2 = 8;
int o = max(input1,input2);When we call max with input1 and input2, they are Argument Values. They are passed to the method parameters int a and int b.
Result is passed to the output variable o.