Java Methods
Give a reusable action a name.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Methods.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A method groups an operation behind a clear name. Separate its inputs, its returned value, and any side effects such as printing. A small method is easier to test when its result depends on explicit parameters.
விரிவான விளக்கங்கள்
Methods-ஐ எப்படி பயன்படுத்துவது?
Method என்பது ஒரு குறிப்பிட்ட task-ஐ பண்றதுக்கான code block — only when called ஆகும்போது மட்டுமே run ஆகும்.
Method declare பண்றதுக்கு:
- Method பேர் வை
- Parentheses
()போடு - Curly braces-க்கு
{}உள்ளே code எழுது
Method Call பண்றது
Method-ஐ use பண்றதுக்கு அதோட பேரை call பண்றோம்:
Static vs. Public Methods
static method — object create பண்ணாமலே call பண்ணலாம்.public method — object create பண்ணி call பண்ணணும்.
static int multiply(int a, int b) {
return a * b;
}
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
System.out.println(multiply(4, 7)); // 28
greet("Alice"); // Hello, Alice!
// Overloading
static double multiply(double a, double b) { return a * b; }
System.out.println(multiply(2.5, 4.0)); // 10.0Java-வில் method overriding-க்கும் method overloading-க்கும் என்ன வித்தியாசம்?
- வித்தியாசமே இல்லை
- Overloading — same name, different parameters. Overriding — subclass-ல் parent method-ஐ redefine செய்வது
- Overriding — compile time. Overloading — runtime
- Overloading only for constructors
Reveal answer / விடை
Overloading — same name, different parameters. Overriding — subclass-ல் parent method-ஐ redefine செய்வது
Overloading: same method name, different parameter types/count (compile-time). Overriding: subclass-ல் parent class method-ஐ new implementation-உடன் redefine செய்வது (runtime polymorphism).
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
static int square(int n) { return n * n; }
public static void main(String[] args) { System.out.println(square(5)); }
}Predict first. Then reveal the expected output / வெளியீடு
25
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Call square with 0, -3, and 6.
Reveal the answer and reasoning / விடை
The results are 0, 9, and 36. The return value is computed from the supplied argument.
தவிர்க்க வேண்டிய தவறு: A non-void method must return an appropriate value along every reachable path that completes normally.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.