ශ්රිත (Reusable Code)
Method එකක් යනු නිශ්චිත කාර්යයක් ඉටු කිරීම සඳහා නිර්මාණය කරන ලද, නැවත නැවත භාවිත කළ හැකි කේත කොටසකි (reusable block of code). Java හි, methods යම් class එකක් තුළ අර්ථ දක්වනු ලැබේ. අපට අවශ්ය ඕනෑම විටක method එකේ නම අඬගැසීමෙන් (calling) එම කේත කොටස ක්රියාත්මක කරවිය හැක.
Method එකක් නිර්මාණය කිරීමේදී, එහි return type (ආපසු ලබාදෙන දත්ත වර්ගය), නම, සහ වරහන් () තුළ parameters (පරාමිතීන්) සඳහන් කරයි. කිසිදු අගයක් ආපසු ලබා නොදෙන්නේ නම්, return type එක void ලෙස යොදයි.
ව්යුහය (Syntax):
[returnType] [methodName]([parameter1], [parameter2], ...) {
// method body - the code to be executed
}
Method එකකට පිටතින් දත්ත ලබා දීමට Parameters භාවිතා කරයි. Method එක නිර්වචනය කිරීමේදී වරහන් තුළ ලබාදෙන විචල්යයන් parameters වේ. Method එක call කරන විට එම parameters සඳහා ලබාදෙන සැබෑ අගයන් Arguments ලෙස හැඳින්වේ.
Method එකකට යම් ගණනය කිරීමක් කර, එහි ප්රතිඵලය ආපසු ලබා දීමට return ප්රකාශය භාවිතා කරයි. return කරන අගයේ දත්ත වර්ගය, method එකේ return type එකට ගැලපිය යුතුය.
සරල සුබ පැතුමක් පෙන්වන, කිසිවක් return නොකරන (void) method එකක්.
public class Example1 {
// Method definition
static void sayHello() {
System.out.println("Hello from a method!");
}
public static void main(String[] args) {
// Calling the method
sayHello();
}
}Hello from a method!නමක් parameter එකක් ලෙස ලබාගෙන, පුද්ගලික සුබ පැතුමක් පෙන්වමු.
public class Example2 {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Kamal"); // "Kamal" is the argument
}
}Hello, Kamal!අංක දෙකක් එකතු කර, එහි ප්රතිඵලය ආපසු ලබා දෙන method එකක්.
public class Example3 {
static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int sum = add(10, 20); // Store the returned value
System.out.println("The sum is: " + sum);
}
}The sum is: 30public class Example4 {
static double calculateArea(double width, double height) {
return width * height;
}
public static void main(String[] args) {
double area = calculateArea(5.5, 10.0);
System.out.println("The area of the rectangle is " + area);
}
}The area of the rectangle is 55.0public class Example5 {
static boolean isAdult(int age) {
return age >= 18;
}
public static void main(String[] args) {
if (isAdult(22)) {
System.out.println("This person is an adult.");
} else {
System.out.println("This person is not an adult.");
}
}
}This person is an adult.public class Example6 {
static String getFullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
public static void main(String[] args) {
String fullName = getFullName("Saman", "Silva");
System.out.println(fullName);
}
}Saman Silvaඑකම නම සහිත, නමුත් විවිධ parameters ඇති methods නිර්මාණය කළ හැක.
public class Example7 {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("Integer sum: " + add(5, 10));
System.out.println("Double sum: " + add(5.5, 10.5));
}
}Integer sum: 15Double sum: 16.0Array එකක් method එකකට යවා, එහි ඇති සියලුම අයිතම print කරමු.
public class Example8 {
static void printArray(String[] arr) {
for (String item : arr) {
System.out.println(item);
}
}
public static void main(String[] args) {
String[] colors = {"Red", "Green", "Blue"};
printArray(colors);
}
}RedGreenBluepublic class Example9 {
static int sumArray(int[] numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
public static void main(String[] args) {
int[] myNumbers = {10, 20, 30, 40};
System.out.println("Sum of the array is: " + sumArray(myNumbers));
}
}Sum of the array is: 100public class Example10 {
static int getSquare(int number) {
return number * number;
}
static void displaySquare(int num) {
int square = getSquare(num); // Calling another method
System.out.println("The square of " + num + " is " + square);
}
public static void main(String[] args) {
displaySquare(5);
}
}The square of 5 is 25