Methods in JAVA


Methods in JAVA

methods in JAVA add modularity and allows code reusability at different place or in modules. It is a very easy way of handling the complete bigger task. A method has basically two parts:

a. Call to the method with correct type and number of arguments

b. Method definitin which may or may not return values.

The syntax of a method definition in JAVA is: 

public static void metodeName(data type arg(s)

{

local declaration of variable(s);

statement(s);

}

Example-1: calling various methods for integer and string processing. Methods have been properly documented to aid in reading and understanding.

package com.example;

public class methosJAVA
{

public static void main(String[] args)
{

average(20, 35);
double average=getAverage(35, 57);
System.out.println(“In main the average=”+average);
//System.out.println();
String fulName=fullName(“Ram”,“Shyam” );
System.out.println(fulName);

}
/**
* // this method calculates average but does not return
* @param value1 the first value
* @param value2 second value
* print average inside the method
*/

public static void average(int value1, int value2)
{

double average =(value1 + value2)/2.0;
System.out.println(“in function Average: “+ average);

}
/**
* This method calculates and returns the average
* @param a first value
* @param b second value
* @return average
*/

public static double getAverage(int a, int b)
{

double average=(a+b)/2.0;
return average;

}
/**
* This method concatenate name and surname
* @param name first value
* @param surname second value
* @return fullName
*/

public static String fullName(String name, String surname)
{

String myName =name + ” ” + surname;
return myName;

}

}

Exmple-2: Checking valid with Boolean

package com.example;
public class methosJAVA
{

public static void main(String[] args)

{

int value =20;
if (isValid(value))
{

System.out.println(“The number is in range”);

}
else

{

System.out.println(“The number is not in range”);

}

}


/**
* This method checks the status of valid data range between 1 and 100
* @param number number to be test in or out pf range
* @return true if number is in range, else return false
*/

public static Boolean isValid(int number)
{

Boolean status;
if (number >=1 && number <= 100)
{

status = true;

}
else
{

status = false;

}
return status;

}

}

We can still make the above program shorter by removing the if else in the method and simply use the return statement as:

package com.example;
public class methosJAVA
{
public static void main(String[] args)
{

int value =20;
if (isValid(value))

{

System.out.println(“The number is in range”);

}
else
{

System.out.println(“The number is not in range”);

}

}

/**
* This method checks the status of valid data
* @param number number to be test in or out pf range
* @return true if number is in range, else return false
*/

//range between 1 and 100
public static Boolean isValid(int number)
{

return number >=1 && number <= 100;

}

}

error: Content is protected !!