5 Java Methods
5 Java Methods
Method
return-type methodName(parameter-list)
//body of method
}
• returnType - It specifies what type of value a method returns For example if a
method has an int return type then it returns an integer value.
• User-defined Methods:
7
Parameters and Arguments
• Parameter is variable defined by a method that receives value when the method is
called.
8
9
Example
return sum;
}
// create a method
public static int square(int num) {
// return statement
Example 3
}
Output:
Square root of 4 is: 2.0
Advantages of using methods
16
What are the advantages of using methods?
• Square of 1 is: 1
• Square of 2 is: 4
• Square of 3 is: 9
• Square of 4 is: 16
• Square of 5 is: 25