Skip to content

Commit a6557c6

Browse files
committed
feat: Add ParametersArguments class to demonstrate method parameters and arguments
Add a program that: - Defines a user-defined method `addNumbers` with parameters - Passes arguments to the method and returns the sum - Demonstrates basic usage of method calls, parameters, and return values in Java.
1 parent 2a57d41 commit a6557c6

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1+
public class ParametersArguments {
2+
//User-defined method to add two numbers
3+
public static int addNumbers(int a, int b) {
4+
return a + b;
5+
}
6+
7+
public static void main(String[] args) {
8+
int result = addNumbers(3, 5);
9+
System.out.println("Sum: " + result);
10+
}
11+
}
12+
113
/* In Java, parameters are the variables listed in the method definition.
214
3-
They act as placeholders for the values that the method can accept. For example, in the method:
15+
They act as placeholders for the values that the method can accept.
16+
17+
For example, in the method:
418
5-
public static int addNumbers(int a, int b)
6-
{
19+
public static int addNumbers(int a, int b) {
720
return a + b;
821
}
922
10-
Here, a and b are parameters. They define what kind of values the method addNumbers can accept (in this case, two integers).
11-
12-
When you call the method, you pass arguments (actual values) to these parameters. For example:
23+
Here, a and b are parameters.
24+
They define what kind of values the method addNumbers can accept (in this case, two integers).
25+
When you call the method, you pass arguments (actual values) to these parameters.
1326
27+
For example:
1428
addNumbers(3, 5);
15-
1629
Here, 3 and 5 are arguments passed to the parameters a and b.
1730
1831
In short:
19-
2032
Parameters are defined in the method signature.
2133
Arguments are the actual values passed to the method when it is called.
2234
2335
Parameters:
24-
2536
Defined in the method signature.
2637
Act as placeholders for the values the method will receive.
2738
2839
Example: In public static void greet(String name), name is a parameter.
2940
3041
Arguments:
3142
The actual values passed to the method when it is called.
32-
3343
Example: In greet("Alice"), "Alice" is an argument passed to the name parameter.
3444
3545
Key Difference:
3646
Parameters are part of the method definition (what the method expects).
3747
Arguments are the actual values you provide when calling the method.
38-
3948
addNumbers(3, 5); → This is a method call that invokes the addNumbers method.
4049
4150
call it a "Method Call" or "Method Invocation
4251
*/
43-
44-
public class ParametersArguments
45-
{
46-
/*User-defined method to add two numbers*/
47-
public static int addNumbers(int a, int b)
48-
{
49-
return a + b;
50-
}
51-
52-
public static void main(String[] args)
53-
{
54-
int result = addNumbers(3, 5);
55-
System.out.println("Sum: " + result);
56-
}
57-
58-
}

0 commit comments

Comments
 (0)