Learn method in java

Hey guys!! Welcome to flower brackets blog. Today we are going to learn method in java.

Learn method in java

A method is a group of statements which executes some logic and returns some value. It can be used multiple times.

Also read – Difference between == and equals method in java

Let’s see syntax of a method,

modifier returnType methodName(Parameter List) 
{
   // method body
}

public static int firstMethod(int x, int y)
{
 
}

In the above syntax,

  • we have “public static” access modifier. public static can be private or private static or protected.

Click here to learn static keyword

  • “int” return type returns integer.
  • “firstMethod” is name of the method and
  • “int x” and “int y” are parameters. They can be left empty or you can pass parameters. Now let’s see an example,
public static int sum(int x, int y)
{
   return x + y;
}

method “sum” has two parameters “int x” and “int y” which returns sum of x and y.


Call a method

There are two ways to call a method, one, method that returns a value and another returning void or nothing.

So when “main” method calls a method, control gets shifted to the called method then the return type returns to the caller.

class MethodDemo
{
   public static void main(String[] args)
   {
      int x = 5;
      int y = 3;
      int result = multiply(x, y);
      System.out.println(result);
   }
   public static int multiply(int a, int b)
   {
      return a * b;
   }
}

Output:

15


Void return type

Void is a keyword in java. Void is generally used if a method simply executes a block of statements which do not return any value.

public class MethodExample 
{
   static void print()
   {
      System.out.println("Hello World!!");
   }

   public static void main(String[] args) 
   {
      print();
   }
}

Output:

Hello World!!


In java, we can pass the values to method in two ways, one, passing by value and another passing by reference.

Passing parameters by value

Passing by value is, when method is called with parameter. Here method parameters are of java primitive type that is, integer, float, char, boolean.

Also read – how to remove element from arraylist in java

If value is changed in “called” method, that change will not reflect in “calling” method. Here variables will be stored in different memory locations and independent of each other.

public class PassingByValue 
{
   public static void main(String[] args) 
   {
      int x = 5;
      System.out.println("Before calling add method : " + x);
      add(x);
   }
   public static void add(int a)
   {
      System.out.println("In add method : " + a);
      a = 2; // value changed here will not affect variable x
      System.out.println("In add method : " + a);
   }
}

Output:

Before calling add method : 5
In add method : 5
In add method : 2


Passing parameters by reference

Now here, when we pass non java primitive type to a method then reference will be passed. For example,

public class PassingByReference 
{
   public static void main(String[] args) 
   {
      int arr[] = {66, 56, 89};
      System.out.println("Before calling add method : " + arr[1]);
      add(arr);
      System.out.println("After calling add method : " + arr[1]);
   }
   // when arr[] is passed to add method, at this point
   // memory address of arr[] is passed to method parameter i.e., num[].
   // Hence memory address of arr[] will be stored in num[] and
   // arr[] and num[] will be pointing to same memory location.

   public static void add(int num[])
   {
      System.out.println("First element : " + num[1]);
      num[1] = 23;
      System.out.println("First element : " + num[1]);
   }
}

Output:

Before calling add method : 56
First element : 56
First element : 23
After calling add method : 23

Related Posts