Saturday 9 July 2016

Java Methods

What is Java Methods :

A java method is nothing but collection of java pre-defined keywords and collection of statements that are group together to perform an operation.The below figure shows the following concept clearly.






















OR

Methods is a piece of code that performs set of operations on operands.Suppose i want to perform an addition operation on two numbers ,what things we should need.

Methods components are..

1-Method Name
2-Return type
3-Passing parameters(Overloading methods)
4-Access Modifiers
5-Collection of statements




















Demo Example:  

The given demo example shows how to make method in Java

package com.navneet.javatutorial;

public class JavaMethods{

public static void main(String[] args) {

Addition.Add(10, 20);    // Calling Add method
System.out.println(Addition.Add(10, 20)); // For Addition

Subtraction.Subtraction(10,20);       //Calling Subtraction Method

System.out.println(Subtraction.Subtraction(10,20));   //For Subtraction

division.Division(10, 20);     // Calling Division method

System.out.println(division.Division(10, 20));   //For Division

Multiplication.Multiplication(10, 20);

System.out.println(Multiplication.Multiplication(10, 20));   //For Multiplication


}

}




class Addition{

 static int Add(int a,int b){
int c=a+b;
return c;
 }
}

class Subtraction{

static int Subtraction(int a,int b){
int c=a-b;
return c;

}

}





class division{


static double Division(double a,double b){
double c=a/b;
return c;

}

}



class Multiplication{

 static float Multiplication(int a,int b){
int c=a*b;
return c;

}



}


O/P Result:

30
-10
0.5
200.0



Types of Methods :

We divide java methods into two broad categories 

1-System Defined Methods (Built-in methods) 

The methods defined inside JavaAPI is the best example of system defined methods for example
main() method ,println() methods and so on.


2-User Defined or Programmer defined methods

The method defined (created ) by the user or programmer is the best example of user defined method.For example ,addition() method described above is user defined method.














1 comment: