Methods Flashcards
returnType
DEFINE:
It specifies what type of value a method returns.
For example, if a method has anintreturn type then it returns an integer value
If the method does not return a value, its return type isvoid
Syntax: accessModifier returnType methodName(){ // method body }
EXAMPLE:
public void printName(String name){
System.out.println(name);
}
methodName
DEFINE:
It is an identifierthat is used to refer to the method in a program, could be any name but should be meaningful to increase readability of your code
Syntax: accessModifier returnType methodName(){ // method body }
EXAMPLE:
public void printName(String name){
System.out.println(name);
}
method body
DEFINE:
It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces{ }
Syntax: accessModifier returnType methodName(){ // method body }
EXAMPLE:
public void printName(String name){
System.out.println(name);
}
method parameters
DEFINE:
Arguments that we pass inside the method parentheses
// method with two parameters public int addNumbers(int a , int b) { return a + b; }
// method with three parameters public int addNumbers(int a , int b , int c){ return a + b + c;
What is method in Java?
A method is a block of code that performs a specific task
Methods are functions or behaviors of the class
There are void and return types of methods
Syntax: accessModifier returnType methodName(){ // method body }
What are the 2 types of methods?
We use thereturn statementto return any value for return type methods EXAMPLE: public int add(int a, int b){ return a + b; }
Other methods are known as void methods and these methods do not return any value EXAMPLE: public void printHello(){ System.out.println(“Hello”); }
Method overloading
RULES:
In Java, two or more methodsmay have the same name
This is allowed if they differ in parameters (different number of parameters, different types of parameters, or both).
EXAMPLE: // method with two parameters public int addNumbers(int a, int b) { return a + b; }
// overloaded method with three parameters public int addNumbers(int a, int b, int c){ return a + b + c;
These methods are called overloaded methods and this feature is called method overloading
Note:Multiple methods can have the same name if the number and/or type of parameters are different
How to achieve method overloading
Method overloading is achieved by either: (argument = parameter)
-changing the number of arguments
EXAMPLE: // method with two parameters public int addNumbers(int a, int b) { return a + b; }
// overloaded method with three parameters public int addNumbers(int a, int b, int c){ return a + b + c; }
-or changing the data type of arguments
EXAMPLE: // method with int parameter public void print(int a) { System.out.println(a); }
// overloaded method with String parameter public void print(String word){ System.out.println(word); }
Advantages of using methods
-Code reusability:
We can write a method once and use it multiple times
-Methods make code morereadable and easierto debug