1-Java Methods
1-Java Methods
Java Methods
Dr. R. A. H. M. Rupasingha
Senior Lecturer in Computer Science
Content
• Java Methods
• Java Method Parameters
• Java Method Overloading
2
Java Methods
• Methods are used to perform certain actions, and they are also
known as functions.
3
How it works?
4
Types of Java Methods
5
Types of Java Methods (Cont.)
1. Standard Library Methods
• The standard library methods are built-in methods in Java that are
already available for use.
• These standard libraries come along with the Java Class Library (JCL)
in a Java archive (*.jar) file with JVM and JRE.
• Eg:
• Java provides some pre-defined methods, such as
System.out.println(). print() is a method of java.io.PrintSteam.
The print("...") prints the string inside quotation marks.
• sqrt()
is a method of Math class. It returns square root of a
number.
6
Types of Java Methods (Cont.)
1. Standard Library Methods (Cont.)
Example 01
public class MyClass
{
public static void main(String[] args) {
System.out.print("Square root of 4 is: " + Math.sqrt(4));
}
}
Output:
Square root of 4 is: 2.0 7
Types of Java Methods (Cont.)
2. User-defined methods
• You can also define methods inside a class as per your wish.
Such methods are called user-defined methods.
• Before you can use (call a method), you need to define it.
• Let’s see how to define methods in Java.
8
Create a Method
• A method must be declared within a class.
• It is defined with the name of the method, followed by
parentheses (). You can pass data, known as parameters, into a
method.
Naming
• The 1st letter should be lower case and then normal camelcase
rules should be use.
• The names should typically be verb, nouns or phases.
• Eg: getBalance(); doCalculate(); setCustomerName();
9
Create a Method (Cont.)
modifier return type name of the method
public class MyClass {
Example 02
static void myMethod() {
Create a method inside MyClass: // code to be executed
}
}
• myMethod() is the name of the method.
• static means that the method belongs to the MyClass class and
not an object of the MyClass class. (You will learn more about
objects and how to access methods through objects later.)
• void means that this method does not have a return value. (You
will learn more about return values later.) 10
Call a Method
• Now you defined a method, you need to use it. For that, you
have to call the method.
• To call a method in Java, write the method's name followed by
two parentheses () and a semicolon;
- Eg: myMethod() ;
• In the following example, myMethod() is used to print a text (the
action), when it is called:
11
Call a Method (Cont.) myMethod();
myMethod();
• Java Methods
• Java Method Parameters
• Java Method Overloading
15
Parameters and Arguments
16
Parameters and Arguments - Example 01
• The following example has a method that takes a String called fname
as parameter. When the method is called, we pass along a first name,
which is used inside the method to print the full name:
type Parameter
public class MyClass {
static void myMethod(String fname) {
System.out.println(fname + " Perera");
}
Output:
public static void main(String[] args) {
myMethod("Kamal"); Kamal Perera
myMethod("Sumudhu"); Sumudhu Perera
myMethod("Nimal"); Nimal Perera
}
} Arguments 17
Multiple Parameters - Example 02
• You can have as many parameters as you like:
Parameter list
public class MyClass {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) { Output:
myMethod("Kamal", 15); Kamal is 15
myMethod("Sumudu", 8); Sumudu is 8
myMethod("Nimal", 10); Nimal is 10
}
} Arguments 18
Components of Method Declarations
• In general, method declarations has five components :
2 3 4
19
Components of Method Declarations (Cont.)
22
Return Values - Example 03
• Java Methods
• Java Method Parameters
• Java Method Overloading
27
Method Overloading
28
Method Overloading public class MyClass {
- Example 01 static int plusMethodInt(int x, int y) {
return x + y;
• Consider the following }
static double plusMethodDouble(double x, double y) {
example, which have two
return x + y;
methods that add numbers
}
of different type:
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
Output System.out.println("double: " + myNum2);
int: 13 }
double: 10.559999999999999 } 29
Method Overloading - Example 01 (Cont.)
public class MyClass {
static int plusMethod(int x, int y) {
• Instead of defining two methods return x + y;
that should do the same thing, }
it is better to overload one. static double plusMethod(double x, double y) {
• In the example below, we return x + y;
}
overload the plusMethod
public static void main(String[] args) {
method to work for both int and
int myNum1 = plusMethod(8, 5);
double: double myNum2 = plusMethod(4.3, 6.26);
• Note: Multiple methods can System.out.println("int: " + myNum1);
have the same name as long as System.out.println("double: " + myNum2);
the number and/or type of }
} Output
parameters are different. int: 13
30
double: 10.559999999999999
Summary
• Java Methods
• How it works?
• Types of Java Methods
• Create a Method
• Call a Method
• Java Method Parameters
• Parameters and Arguments
• Multiple Parameters
• Components of Method Declarations
• Return Values
• A Method with If...Else
• Java Method Overloading 31
End. 32