Commit ae03507
committed
feat: Add MethodPractice to demonstrate method variations with max and inc
WHAT the code does:
Defines a MethodPractice class with:
- A commented-out static max(int x, int y) method that returns the larger of two integers.
- A static inc(int x) method that increments its parameter and prints the incremented value.
In main():
- The max() method call is commented out but shows how to compare two integers.
- The inc() method is called with a = 10, printing 11 inside the method, then printing 10 again outside to illustrate call by value.
WHY this matters:
Highlights Java’s call by value semantics for primitive types:
- Changes to parameters inside a method do not persist outside.
Contrasts return-based methods (like max) with void methods that print results directly.
Demonstrates how experimenting with commented code can help explore method design options.
HOW it works:
If max() were active:
- It would compare two integers and return the larger value.
The active inc() method:
- Accepts a copy of the argument
- Increments and prints the local value
- Leaves the original variable in main() unchanged
Outputs show "11" inside the method and "10" in main().
Tips and gotchas:
Printing inside utility methods (like inc) is less flexible than returning a value; returning allows reuse in calculations.
max() is better expressed using Math.max(x, y), but implementing it builds understanding of method logic.
Commented code can serve as a teaching aid, but in production, it’s better tracked with version control history.
Use-cases:
Educational exploration of method design in Java.
Practicing static method declaration and invocation.
Demonstrating the distinction between return-based and side-effect-based methods.
Short key: class-methodpractice call-by-value method-design.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent cff1a59 commit ae03507
1 file changed
+1
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | 1 | | |
5 | | - | |
6 | 2 | | |
7 | 3 | | |
8 | 4 | | |
| |||
11 | 7 | | |
12 | 8 | | |
13 | 9 | | |
14 | | - | |
15 | | - | |
| 10 | + | |
16 | 11 | | |
17 | 12 | | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
21 | | - | |
22 | 16 | | |
23 | 17 | | |
24 | 18 | | |
| |||
28 | 22 | | |
29 | 23 | | |
30 | 24 | | |
31 | | - | |
32 | | - | |
33 | 25 | | |
34 | 26 | | |
0 commit comments