Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Methods

Methods in java are a chunk of Code that is used to conduct a particular task.


How the Methods look like?

return_type methodName(dataType parameter1, dataType parameter2,...) {

  <Code Block>
  return <return_variable>;
}

Note : Do not get scared by the above line of code. We will make it simpler.


  1. A java method can be of any name(i.e. methodName mentioned above), followed by parentheses, i.e. ().

  2. Inside the parentheses, you can specify any number of parameters,(i.e. dataType parameter1, dataType parameter2,... mentioned above).

  3. The block of code of the method should be inside the curly braces, i.e. {}.

  4. There is a return statement which is an optional statement and returns the calculated result to the caller(i.e. return <return_variable>)..

  5. There is also a return type, which specifies the datatype of the returned value(i.e. return_type ).


Below is how an add method would look like


int add(int firstNumber, int secondNumber) {

  int result;

  result = firstNumber + secondNumber;
  return result;
}



Example :

Say you are given a task(just like java method) of adding two numbers. You would follow the below steps to achieve it :


  1.  You would ask the firstNumber and secondNumber from the person who has given you this task.
  2. And the above java method expects the same thing.


    int add(int firstNumber, int secondNumber)

    Here, 'int firstNumber' and 'int secondNumber' are the two numbers which a java method expects just like you.

    Also 'add' is the name of the method. And 'int' datatype also called as return type tells what type of value would the result be(In this case it is a number).


  3.  Then you would add the numbers and give the added value to the caller.
  4. Same thing is done by the java method.

    It adds 'firstNumber' and 'secondNumber' and stores the added value in a variable called 'result'.


    result = firstNumber + secondNumber;

    It then returns the 'result' to the caller(We will see next, who is a caller).


    return result;

How would you call the add method?

The method can be called from the main method.


public class Test{
  public static void main(String[] arg){

    int firstNum = 5;
    int secondNum = 8;
    int addedResult;

    addedResult = add(firstNum,secondNum);

    System.out.println("The added value is : "+addedResult);
  }

  //Add method starts here.
  static int add(int firstNumber, int secondNumber) {

    int result;
    result = firstNumber + secondNumber;
    return result;
  }
}

Output :


   The added value of two numbers is : 13

Note : We have added 'static' keyword in front of the method, so that it could be accessed from main method.


In our main method we have called the 'add' method :


addedResult = add(firstNum,secondNum);

So, there are two parameters, firstNum and secondNum that contains the actual values 5 and 8.


int firstNum = 5; int secondNum = 8;

Now, java searches for the 'add' method with two parameters.


int add(int firstNumber, int secondNumber)

It finds the add method and assigns the value of firstNum to firstNumber and secondNum to secondNumber.

java_method

Now, in the actual method firstNumber has 5 and secondNumber has 8. So, it proceeds with the addition and returns the added result to the caller.


static int add(int firstNumber, int secondNumber) {

  int result;
  result = firstNumber + secondNumber;
  return result;
}

So, the 'addedResult' has the returned added result which it prints in the next statement.


void Return Type

Say, you have a method that prints just "Hello World" on the screen. So, it does not return anything. In that case it should not have a return type as well.

In such cases where there is no return type in a method, we put it as 'void'.


void printSometing() {

  System.out.println("Hello World");
}