Methods in Java

HOME

A method is a block of code that is use to perform certain actions. Method runs when it called. Method allow us to reuse the code.

Types of methods in Java

    • Standard Library methods
    • User defined methods

Standard Library Method
They are built-in Java methods are readily available to use. For example, sqrt() is a Method of Math and returns square root of a number.

User-defined Method
User can define a method inside a class as per his/her wish.

How to create a Method?

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

Here,

  • modifier − It defines the access type of the method and it is optional to us
  • returnType − Method may return a value or not. Void does not return any value.
  • nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
  • Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
  • method body − The method body defines what the method does with the statements.
public static int methodName(int a, int b) {
      // body
   }
 
public class Method_Example {
     public static void main(String[] args) {
              
       //Standard Library Method
       System.out.println("Square root of 9 is : "+Math.sqrt(9));
             
        //User defined Method
       MyMethod();
   }

   public staticvoid MyMethod()
   {
       System.out.println("My User defined Method");
   }
}

Output
Square root of 9 is: 3.0
My User defined Method

Java Methods with Arguments and Return Value

A Java method can have zero or more parameters, and they may return a value.

public class Min_Method {
      public static void main(String[] args) {
         int a = 10;
         int b = 5;
         int c = minMethod(a,b
         System.out.println("Minimum value is :"+c);                  
      }
          
      //Define function min_function
      public static int minMethod(int x, int y)
        {
           int min;
           if (x>y)
              min =y;
           else
              min = x;
      //return value
           return min;
     }
}

Output
Minimum value is :5

Void Keyword

The void keyword allows us to create methods that do not return a value. 

public class Min_Method { 
    public static void main(String[] args) {
 
      int a = 10;
      int b = 5;
 
     //Call function
        minMethod(a,b);             
  }       
  
   //Define function min_function
     public static void minMethod(int x, int y)
       {
           intmin;
           if(x>y)
              min =y;
           else
               min = x;
           System.out.println("Minimum value is :"+min);
        }
     }

Output
Minimum value is :5

Method Overloading

When a class has two or more methods by the same name but different parameters, it known as method overloading. To know more about method overloading, click

public class Overloading_Example {
   public static void main(String[] args) {
 
      int a= 10;
      int b = 24;
      double c = 12.0;
      double d = 13.0;  
      min_function(a, b);
      min_function(c,d);
   }
 
   public static void min_function(int x, int y)
    {
         int min;
         if(x>y)
           min = y;
         else
            min = x;
 
          System.out.println("Minimum value for integer is :"+ min);
       }
 
   public static void min_function(double m, double n)
     {
          double min;
          if(m>n)
              min=n;
          else
               min=m;
          System.out.println("Minimum value for integer is :"+ min);
        }
   }
 
Output
Minimum value for integer is :10
Minimum value for integer is :12.0

What is main method in Java?

The main()method is the entry point into the application. The signature of the method is always: public static void main(String[] args) 

One thought on “Methods in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s