Java Methods: Your Ultimate Guide
👋
Hey there, future coding superstar!
🚀
Let’s dive into the awesome world of Java Methods and
make your code cleaner, smarter, and way more fun to write. No more repetitive tasks – we’re
about to level up your programming game!
What is a Method? 🤔
Imagine you’re trying to print “Welcome” 10 times. Without methods, you’d be doing something
like this:
[Link]("Welcome");
[Link]("Welcome");
[Link]("Welcome");
[Link]("Welcome");
[Link]("Welcome");
// ...and so on, 5 more times!
Ugh, right? 😩 Lots of repeated code! That’s where methods come to the rescue. Instead, we
can create a super-handy method:
void welcome() {
[Link]("Welcome");
}
Now, whenever you need to say “Welcome,” you just call your method like a boss:
welcome();
welcome();
welcome();
See? So much cleaner, saves you a ton of time, and makes your program way easier to read
and manage. ✨
Real-Life Example: The “Bring Water” Method 💧
Think about it like this: when your mom asks you to “Bring water,” she doesn’t explain every
single step, right?
👉 Go to the kitchen 👉 Take a glass 👉 Fill it with water 👉 Bring it back
She just says: “Bring Water” 🗣️
That simple instruction is like a method name. All the detailed work is hidden inside it! Similarly,
in programming, square() , sum() , and display() are all method names that encapsulate
a specific task.
Structure of a Method: The Blueprint 📐
Every method has a basic structure. It’s like its DNA:
returnType methodName(parameters) {
// statements (the magic happens here!)
}
Let’s break down an example:
int square(int n) {
return n * n;
}
Part 1: Return Type 🎁
This tells Java what kind of value the method will send back after it’s done its job. In our
square example:
int square(int n) {
return n * n;
}
It’s returning an int (an integer). Easy peasy!
Other common return types you’ll bump into:
double (for decimal numbers)
char (for single characters)
String (for text)
boolean (for true/false)
void (if the method doesn’t return anything at all – it just does something)
Example of a char return type:
char getGrade() {
return 'A';
}
Part 2: Method Name 🏷️
This is simply the name you give your method. It should be descriptive so you know what it
does! Like square , sum , or display .
In int square(int n) , square is our method name.
Part 3: Parameters 🤝
Parameters are like little gifts of information you give to your method so it can do its work.
They’re values the method receives.
In int square(int n) , n is our parameter.
Why Parameters are Your Best Friends 💖
Imagine a square() method without parameters:
void square() {
[Link](5 * 5);
}
This method will always give you 25. What if you want the square of 7, 9, or 11? You’d have to
write a new method for each! 😫
But with parameters, it’s a game-changer:
int square(int n) {
return n * n;
}
Now, you can get the square of any number just by passing it in:
square(5); // Gives 25
square(7); // Gives 49
square(9); // Gives 81
So much more flexible and powerful! 💪
Method Call: Making the Magic Happen 🪄
Methods don’t just run by themselves. You have to call them to make them do their thing!
square(5);
This line right here? That’s a Method Call! You’re telling your program, “Hey, run the square
method and give it the number 5!”
Four Types of Methods: The Core Four! 🌟
These are super important, especially if you’re prepping for exams! Get ready to ace them. 🔥
TYPE 1: No Arguments + No Return Value
Definition: This method takes nothing in and gives nothing back. It just performs an action.
void greet() {
[Link]("Good Morning");
}
Call:
greet();
Output:
Good Morning
Complete Program Example:
class Demo {
void greet() {
[Link]("Good Morning");
}
public static void main(String args[]) {
Demo obj = new Demo();
[Link]();
}
}
TYPE 2: Arguments + No Return Value
Definition: This method receives values (arguments) but doesn’t return anything. It uses the
values to perform an action.
void square(int n) {
[Link](n * n);
}
Call:
square(5);
Output:
25
Complete Program Example:
class Demo {
void square(int n) {
[Link](n * n);
}
public static void main(String args[]) {
Demo obj = new Demo();
[Link](8);
}
}
Output:
64
TYPE 3: Arguments + Return Value
Definition: This is a powerhouse! It takes values (arguments) and returns a result. MOST
IMPORTANT FOR ICSE! 🔥
int square(int n) {
return n * n;
}
Call:
int ans = square(5);
Output: (The value 25 is stored in ans )
25
Complete Program Example:
class Demo {
int square(int n) {
return n * n;
}
public static void main(String args[]) {
Demo obj = new Demo();
int x = [Link](5);
[Link](x);
}
}
Output:
25
TYPE 4: No Arguments + Return Value
Definition: This method returns a value but doesn’t need any input to do so.
int number() {
return 100;
}
Call:
int x = number();
Output: (The value 100 is stored in x )
100
Formal vs. Actual Parameters: The Naming Game 🤓
This can sometimes trip people up, but it’s super simple!
Formal Parameter
This is the variable name you use in the method definition.
int square(int n) // <-- 'n' is the Formal Parameter
Actual Parameter
This is the actual value you pass in when you call the method.
square(5); // <-- '5' is the Actual Parameter
How Control Moves: The Method Journey 🗺️
Ever wonder what happens behind the scenes when you call a method? Let’s trace it with int
x = square(5);
1. Step 1: Method Called The program sees square(5); and knows it needs to jump to
your square method.
2. Step 2: Value Copied The value 5 is copied into the method’s parameter n . So, n = 5 .
3. Step 3: Calculation Inside the square method, return n * n; calculates 5 * 5 .
4. Step 4: Returns The method sends the calculated result ( 25 ) back to where it was called
from.
5. Step 5: Stored The returned value ( 25 ) is then stored in the variable x . So, x = 25 .
Done! ✅ Your method has successfully completed its mission!
Practice Examples: Let’s Get Coding! 💻
Here are a few more examples to solidify your understanding:
Example 1: Sum of Two Numbers
int sum(int a, int b) {
return a + b;
}
Call:
int s = sum(10, 20);
Output: (The value 30 is stored in s )
30
Example 2: Maximum of Two Numbers
int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
Call:
max(15, 20);
Output: (The value 20 is returned)
20
Example 3: Even or Odd Checker
void check(int n) {
if (n % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
Call:
check(17);
Output:
Odd
Example 4: Area of a Rectangle
int area(int l, int b) {
return l * b;
}
Call:
area(10, 5);
Output: (The value 50 is returned)
50
Example 5: Cube of a Number
int cube(int n) {
return n * n * n;
}
Call:
cube(4);
Output: (The value 64 is returned)
64
Board Exam Frequently Asked Theory Questions 🎓
Get ready for these common questions!
What is a user-defined method? A method created by the programmer to perform a
specific task.
What is method calling? The process of invoking or executing a method.
What is a parameter? A variable that receives a value from the calling statement.
What is a return statement? A statement used to send a value back to the calling method.
What is void ? A return type indicating that no value is returned by the method.
Golden Rules for ICSE: Remember These! ✨
These little nuggets of wisdom will help you quickly understand methods:
Whenever you see return ➡ The method returns something.
Whenever you see void ➡ The method returns nothing.
Whenever you see (int n) ➡ The method accepts data (a parameter).
Whenever you see () ➡ The method accepts no data (no parameters).
Quick Memory Table: Your Cheat Sheet! 📝
Arguments Return Example
No No void show()
Yes No void show(int n)
No Yes int getNum()
Yes Yes int square(int n)
That’s it, champ! You’ve just crushed Java Methods. Keep practicing, and you’ll be a coding
wizard in no time!🧙♂️💻