Java Methods, Recursion and Method Overloading

Ad Code

Java Methods, Recursion and Method Overloading

 


What is a method?

method is a block of code which only runs when it is called. Methods are used to perform certain actions, and they are also known as functions.

Method creation

method must be declared within a class and defined with the name of the method, followed by parentheses ().

Types of Java methods

There are two types of methods in Java.

  • Standard Library Methods: The standard library methods are built-in methods in Java that are readily available for use.
  • User-defined Methods: User-defined methods can create our own choice to perform some task.

Syntax for declaring a method

public class MyClass {
   static void myMethod() {
    // code to be executed
  }
}
  • myMethod() is the name of the method
  • static means method belongs to the MyClass class and not an object of the MyClass class.
  • void means that this method does not have a return value.

Method calling

To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
Example

public class MyClass {
   static void myMethod() {
    System.out.println("Function execution!");
  }
   public static void main(String[] args) {
    myMethod();
  }
}

Output

   Function execution!


Method Parameters and Arguments

Parameters act as variables inside the method and information can be passed to methods as parameter.
Example-1 of SI calculation

import java.util.Scanner;
public class MyClass {
   public static int myMethod(int m, int n, int p) {
     return (m*n*p)/100;
   }
   public static void main(String[] args) {
    Scanner obj=new Scanner(System.in);
    System.out.println("Enter value of p,r and t");
    int p=obj.nextInt();
   int r=obj.nextInt();
   int t=obj.nextInt();
   int si=myMethod(p,r,t);
   System.out.println("SI="+si);
  }
}


Output

   Enter value of p,r and t
   5000
   4
   2
   SI=400

Example-2 of SI calculation

import java.util.Scanner;
public class MyClass {
   public static void main(String[] args) {
    Scanner obj=new Scanner(System.in);
    System.out.println("Enter value of p,r and t");
    int p=obj.nextInt();
   int r=obj.nextInt();
   int t=obj.nextInt();
   int si=myMethod(p,r,t);
   System.out.println("SI="+si);
  }
   public static int myMethod(int m, int n, int p) {
     return (m*n*p)/100;
   }
}


Output

   Enter value of p,r and t
   5000
   4
   2
   SI=400

Java Method Overloading

Multiple methods can have the same name with different parameters is called method overloading.
Example

import java.util.Scanner;
public class MyClass {
   static int sum(int x, int y) {
     return x + y;
   }
   static double sum(double x, double y) {
     return x + y;
   }
   public static void main(String[] args) {
   int n1 = sum(7, 5);
   double n2 = sum(5.26, 6.30);
   System.out.println("int: " + n1);
   System.out.println("double: " + n2);
  }
}


Output

   int: 12
   double: 11.559999999999999

Java Recursion

Recursion is the mechanism of making a function call itself, that means the process in which a function calls itself directly or indirectly.
Example of Factorial without recursion

class FactorialExample{
  public static void main(String args[]){
    int i,fact=1;
    int number=5;//It is the number to calculate factorial
   for(i=1;i<=number;i++){
     fact=fact*i;
   }
    System.out.println("Factorial of "+number+" is: "+fact);
   }
}


Example of Factorial with recursion

class FactorialExample2{  
 static int factorial(int n){    
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int i,fact=1;  
  int number=4;//It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  

   int: Factorial of 5 is: 120















Post a Comment

0 Comments

Ad Code