08- Method return types in java

  • Previously we wrote methods that having void type. now lets see other return types and how to do that.

public class Testing {

    int m(){
        System.out.println("m method");
        int x=10;
        return x;
    }

    public static void main(String args[]){
        Testing t = new Testing();
        int val=t.m();
        System.out.println(val);
    }
}

Now lets try to write a code for get the average for a given three decimal numbers.

public class Testing {

      int add(int x, int y, int z){
           return (x+y+z);
      }

      double average(int x){
           return (x/3);
      }

      public static void main(String args[]){
          Testing t = new Testing();
          int val=t.add(12,45,67);
          double avg=t.average(val);

          System.out.println("The average is" + avg);
     }
}

Leave a comment