Method in Java Flashcards
what is method ?
A method is a block of code that or collections of instructions that perform a specific task, and return the result to the caller, when the method is called
name the types of method.
The types of method are:
1.predefined method: method that is already defined in the Java class libiaries.
- User defined method: the method written by the user or programmer
what is a parameter?
A variable which is part of a method signature.
parameter is a place holder in the definition of a function for a value that will later be passed into a function/method when a function is called.
parameter act as variable in a method
what is a Argument?
Argument are the actual value given to the method when a method is called.
what is pass by value and pass by reference?
what is the syntax for a method
public static (return type) (method name)(parameter){
// method body
}
Example: public static int getnumber(int num1 , num2){
return num1 + num2;
{
why use method?
- to reuse code
- define the code once and use
it many time
How to call a method
to call a method in Java, write the method’s name followed by two parentheses() and a semicolon.
Example: myMethod();
can a method be called one time?
no, a method can be call many time as you want.
what are value returning method?
A method that return a value .
the return type is the type of the data that will be returned.
Example: public static int getNumber(int num2, num3){
// this method return type is an int.
}
what is method overloading?
method overloading is writing the same method with different parameter.
1. overloaded method can have different return type.
2. overloaded method must have different parameter.
example: public static void sayHi(){
System.out.println( “hi”);
}
public static String sayHi(){
return “hi”;
}
What is the return keyword?
A keyword that is used to return a value from a method.