🟡 Control Flow · Lesson 15
Methods in Java
Methods in Java
What is a Method?
A method is a reusable block of code that performs a task. It can take parameters and return a value.
Example
public class Calc {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int sum = add(5, 3);
System.out.println("Sum: " + sum);
}
}Sum: 8
Method Overloading
Same method name with different parameters = overloading.
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
Summary
- Methods are reusable code blocks with parameters and a return type.
- Overloading = same name, different parameters.
Method क्या है?
Method code का reusable block है जो एक task करता है। यह parameters ले सकता है और value return कर सकता है।
Example
public class Calc {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int sum = add(5, 3);
System.out.println("Sum: " + sum);
}
}Sum: 8
Method Overloading
एक ही method नाम अलग parameters के साथ = overloading।
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
सारांश
- Methods reusable code blocks हैं, parameters और return type के साथ।
- Overloading = एक नाम, अलग parameters।