Java for beginners
407 subscribers
49 photos
29 links
Learn Java daily with bite-sized lessons, practical code examples, output previews, and fun quizzes.
Download Telegram
Here's your Telegram post for Day 6!

---

Hey #JavaLearners! 👋 Ready for Day 6 of our Java journey? Today, we're diving into the heart of code reusability: **Methods in Java**! 🚀

Methods are like mini-programs within your main program. They help you organize code, perform specific tasks, and make your applications much cleaner and easier to manage. Think of them as actions your objects can perform! ✨ They take input, do some work, and optionally return a result. Let's see them in action! 👇

```java
public class MethodMagic {

public static void main(String[] args) {
// Calling our custom methods
int sumResult = addNumbers(10, 5); // Calling a method with a return value
System.out.println("Sum of 10 and 5 is: " + sumResult);

sayHello("Alice"); // Calling a method with no return value (void)
sayHello("Bob");
}

/**
* This method takes two integers as parameters and returns their sum.
*/
public static int addNumbers(int num1, int num2) {
int sum = num1 + num2;
return sum; // 'return' sends the result back
}

/**
* This method takes a String name as a parameter and prints a greeting.
* It does not return anything, indicated by the 'void' keyword.
*/
public static void sayHello(String name) {
System.out.println("Hello, " + name + "! Welcome to Methods!");
}
}
```

Output example:
```
Sum of 10 and 5 is: 15
Hello, Alice! Welcome to Methods!
Hello, Bob! Welcome to Methods!
```

**Small challenge:**
Your turn! 🛠️ Try creating a new method in the `MethodMagic` class called `multiplyNumbers` that takes two integers as parameters and returns their product. Then call it from your `main` method and print the result! Good luck, future Java wizard! 🧠✨

---