Java Methods: Types, Syntax, and Examples

📘 Core Java – Day 10 Topic: Types of Methods in Java In Java, a method is a block of code that performs a specific task and is defined inside a class. Methods help achieve code reusability, modularity, and readability. 🔹 Method Signature Syntax: returnType methodName(parameters) { // method body } Explanation: methodName: Name of the method (can be any valid identifier) parameters: Inputs passed to the method method body: Code that performs the task returnType: Type of value returned after execution 🔸 Types of Methods in Java Java methods are classified based on input (parameters) and output (return value). 1️⃣ No Input and No Output Method Does not take any parameters Does not return any value Used mainly for displaying messages or simple tasks Example: void greet() { System.out.println("Welcome to Core Java"); } 2️⃣ No Input and Output Method Does not take any parameters Returns a value Used when the result is generated internally Example: int getNumber() { return 100; } 3️⃣ Input and No Output Method Takes parameters as input Does not return any value Used to perform operations like printing results Example: void printSquare(int num) { System.out.println(num * num); } 4️⃣ Input and Output Method Takes parameters as input Returns a value Most commonly used in real-world applications Example: int add(int a, int b) { return a + b; } 📌 Learning Core Java step by step — Day 10 completed! #CoreJava #JavaProgramming #LearningJourney #Day10 #ProgrammingBasics #StudentDeveloper

To view or add a comment, sign in

Explore content categories