Skip to content

Commit ae03507

Browse files
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

File tree

1 file changed

+1
-9
lines changed

1 file changed

+1
-9
lines changed
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
package methodpractice;
2-
3-
41
public class MethodPractice {
5-
62
/*static int max(int x,int y)
73
{
84
if(x>y)
@@ -11,14 +7,12 @@ public class MethodPractice {
117
return y;
128
}*/
139

14-
static void inc(int x)
15-
{
10+
static void inc(int x) {
1611
x++;
1712
System.out.println(x);
1813
}
1914

2015
public static void main(String[] args) {
21-
2216
/*int a=10,b=15;
2317
//System.out.println(max(a,b));
2418
@@ -28,7 +22,5 @@ public static void main(String[] args) {
2822
int a=10,b=15;
2923
inc(a);
3024
System.out.println(a);
31-
32-
3325
}
3426
}

0 commit comments

Comments
 (0)