Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

13. Methods and Recursion in Java

 

Methods in Java

  • A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation.
  • It is used to achieve the reusability of code i.e. we write a method once and use it many times.
  • It also provides the easy modification and readability of code.
  • The method is executed only when we call or invoke it.
  • The most important method in Java is the main() method.
  • A method is a function written inside a class.
  • Java is an object-oriented language, we need to write the method inside some class.

Method Declaration

Generally, the following are the components of method declaration:

  • Access Modifier: Defines access type of the method i.e. from where it can be accessed in our application.
  • Return Type: The data type of the value returned by the method or void if does not return a value.
  • Method Name: It is a unique name that is used to define the name of a method. Generally, it should be corresponding to the functionality of the method. A method is invoked or called by its name.
  • Parameters: Comma separated list (for more than one input parameter) of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, we must use empty parenthesis ().
  • Method Body: It is enclosed between curly braces. It contains all the actions to be performed.

Method Signature: It consists of the method name and a parameter list.

Calling a Method

  • The method needs to be called for using its functionality.
  • A method can be called by creating an object of the class in which the method exists followed by the method call.

Static Method

  • A method that belongs to a class rather than an instance of a class.
  • We can create a static method by using the keyword static before the method name.
  • The static method can be called without creating an object. It is invoked by using the class name.

Instance Method

  • The method of the class is known as an instance method.
  • It is a non-static method defined in the class.
  • An instance method can be called by creating an object.
class Func {
    int function(int a, int b) { // Instance Method
        if (a > b) {
            return (a + b);
        } else {
            return ((a + b) * 5);
        }
    }

    static int func(int a, int b) { // Static Method
        if (a > b) {
            return (a + b);
        } else {
            return ((a + b) * 5);
        }
    }
}

class Methods {
    static int func(int a, int b) { // Static Method
        if (a > b) {
            return (a + b);
        } else {
            return ((a + b) * 5);
        }
    }

    int function(int a, int b) { // Instance Method
        if (a > b) {
            return (a + b);
        } else {
            return ((a + b) * 5);
        }
    }

    public static void main(String[] arr) {
        int a = 5, b = 15;
        Main ob = new Main();
        System.out.println("Method in same class called by creating object = " + ob.function(a, b));
        System.out.println("Static Method in same class = " + func(a, b));
        Func obj = new Func();
        System.out.println("Method in other class called by creating object = " + obj.function(a, b));
        System.out.println("Static Method in other class called by class name = " + Func.func(a, b));
    }
}

Output :

Method in same class called by creating object = 100
Static Method in same class = 100
Method in other class called by creating object = 100
Static Method in other class called by class name = 100

Recursion in Java

  • The process in which a method calls itself continuously is called recursion.
  • A method in java that calls itself is called a recursive method.
  • Represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion.
  • If the base case is not reached or not defined, then the stack overflow problem may arise.
class Recursion {
    static int fib(int n) {
        if (n == 1)
            return 0;
        else if (n == 2)
            return 1;
        else
            return (fib(n - 1) + fib(n - 2));
    }

    static int fact(int n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return (fact(n - 1) * n);
    }

    public static void main(String[] arr) {
        System.out.println("The 10th term of fibonacci series = " + fib(10));
        System.out.println("The Fibonacci series for first 10 number is...");
        for (int i = 1; i <= 10; i++) {
            System.out.print(fib(i) + " ");
        }
        System.out.println("\nThe factorial of 5 is = " + fact(5));
    }
}

Output :

The 10th term of fibonacci series = 34
The Fibonacci series for first 10 number is...
0 1 1 2 3 5 8 13 21 34
The factorial of 5 is = 120


Previous

Next

Comments