User defined methods in java

User defined methods in java: Functions are called methods in java. Like in C, java programming has similar user defined methods. There are basically four types of user defined methods. They’re:

  1. Methods with parameter and with return type
  2. Methods with parameter and without return type
  3. Methods without parameter and with return type
  4. Methods without parameter and without return type
User defined methods in java
fig: User defined methods in java

User defined methods in java

Methods with parameter and with return type

private static void addition(int a, int b) {
        int sum = a + b;
        System.out.println("The sum of two number is : " + sum);
    }

The above method consists of two parameters and it doesn’t return any value so there is a return type void. The number of parameters and its types can be any as per the requirement. The static keyword is used because I’ve called this method from static main method (you will see below in complete code). If you’re calling the method from non-static method then, no need to use static keyword. 

Methods with parameter and without return type

private static int difference(int a, int b) {
        int diff;
        if(a>b) {
            diff = a-b;
            return diff;
        } else {
            diff = b-a;
            return diff;
        }
    }

The above method returns int type value. The return type can be any as per the requirement. The rest things are as of above.  

Methods without parameter and with return type

private static int multiply() {
        int i = 10, j= 5;
        int mul = i * j;
        return mul;
    }

The method doesn’t take any parameter as input but, it will perform certain action and return the value, whenever it is called. The return type can be any.

Methods without parameter and without return type

private static void division() {
        int i = 1, j = 6;
        int div = j/i;
        System.out.println("The divison of two number is : " + div);
    }

The method neither takes any parameters nor returns any value. It just performs the action, whenever it is called. So the return type in above method is void, which means nothing to return.

The complete program is given below:


package test;

public class Test {

    public static void main(String[] args) {
        int a = 10, b = 20;
        addition(a,b);
        
        int diff = difference(a,b);
        System.out.println("The difference of two number is : " + diff);
        
        int mul = multiply();
        System.out.println("The multiplication of two number is : " + mul);
        
        division();
    }

    private static void addition(int a, int b) {
        int sum = a + b;
        System.out.println("The sum of two number is : " + sum);
    }

    private static int difference(int a, int b) {
        int diff;
        if(a>b) {
            diff = a-b;
            return diff;
        } else {
            diff = b-a;
            return diff;
        }
    }

    private static int multiply() {
        int i = 10, j= 5;
        int mul = i * j;
        return mul;
    }

    private static void division() {
        int i = 1, j = 6;
        int div = j/i;
        System.out.println("The divison of two number is : " + div);
    }
    
}

That’s it. Congrats you’ve learn one more thing in Java.