DMCA.com Protection Status Java Programming Lesson 7 Part-2 | Functions / Methods in Java for Beginners - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 7 Part-2 | Functions / Methods in Java for Beginners

LESSON-7 PART-2

Functions / Methods in Java



✅ Here is Lesson-7: Functions / Methods in Java — Simple to Advanced 🚀


📘 Lesson-7: Functions / Methods

A method is a block of code that performs a specific task and can be reused.


✅ Why Methods?

✔ Avoid repeating code
✔ Make programs cleaner
✔ Easier to debug
✔ Improve organization


✅ Syntax of a Method

returnType methodName(parameters) { // code return value; // (only if returnType is not void) }

Example:

void greet() { System.out.println("Hello Java!"); }

✅ Types of Methods

1️⃣ Method with No return, No parameters

public static void greet() { System.out.println("Welcome to Java!"); }

Call:

greet();

2️⃣ Method with parameters

static void greetUser(String name) { System.out.println("Hello, " + name); }

Call:

greetUser("Jitu");

3️⃣ Method with return value

static int add(int a, int b) { return a + b; }

Call:

int result = add(10, 20); System.out.println(result);

4️⃣ Method Overloading

Same method name but different parameters ✅

static int add(int a, int b) { return a + b; } static double add(double a, double b) { return a + b; }

✅ Full Example Program

public class Main { static void sayHello() { System.out.println("Hello Java"); } static String fullName(String first, String last) { return first + " " + last; } static int multiply(int a, int b) { return a * b; } public static void main(String[] args) { sayHello(); System.out.println(fullName("S.M.", "JITU HASAN")); int result = multiply(5, 4); System.out.println("Multiplication = " + result); } }

✅ Method Calling Rules

Feature       Keyword
Used inside main without creating object     static
Needs object to call         Non-static

Example (non-static):

class Test { void msg() { System.out.println("Hello"); } public static void main(String[] args) { Test t = new Test(); t.msg(); } }

🧠 KEY TAKEAWAYS

✔ Methods = Reusable code
✔ Parameters send data → return values bring data back
✔ Static methods → direct call
✔ Non-static methods → require object
✔ Method Overloading allowed ✅


📝 Practice Exercise

Write a program with:

  • Method add() → returns sum

  • Method sub() → returns subtraction

  • Method display() → prints your full name

📌 Then call them from main()


✅ Lesson-7 PART-2 Completed!



➡ NEXT Lesson-7 Part-3: Scanner Input (more)

1 comment:

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.