Methods Flashcards

1
Q

returnType

DEFINE:

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

methodName

DEFINE:

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

method body

DEFINE:

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

method parameters

DEFINE:

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is method in Java?

A

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
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 2 types of methods?

A
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”);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method overloading

RULES:

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to achieve method overloading

A

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);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Advantages of using methods

A

-Code reusability:
We can write a method once and use it multiple times

-Methods make code morereadable and easierto debug

How well did you know this?
1
Not at all
2
3
4
5
Perfectly