Java - Methods

A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.

Syntax :

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

							  

General Syntax :


Example :

							  
public int methodName(int a, int b) {
  // body
}
							  
							  

Here,
  • public : modifier.
  • int: return type
  • methodName: name of the method
  • int a, int b: list of parameters

General Diagram :

Example :

							  
public class ExampleMinNumber{
   
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      int c = minFunction(a, b);
      System.out.println("Minimum Value = " + c);
   }

   /** returns the minimum of two numbers */
   public int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
}


Minimum Value = 6

BytecodeTechnosolutions.com | Raju