-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path099-Call-by-Value.java
More file actions
63 lines (50 loc) · 2.02 KB
/
Copy path099-Call-by-Value.java
File metadata and controls
63 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* ============================================================================
* Program Name : Call by Value
* File Name : 099-Call-by-Value.java
* Class Name : CallByValue
*
* Description:
* This program demonstrates the Call by Value concept in Java.
* Java always passes arguments by value. Changes made to the
* method parameters do not affect the original variables.
*
* Objective:
* - Understand the Call by Value concept.
* - Learn how method arguments are passed in Java.
* - Observe that original variable values remain unchanged.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
public class CallByValue {
// User-defined method to demonstrate Call by Value.
public void modifyValue(int number) {
// Display the received value.
System.out.println("Value Inside Method (Before Modification): " + number);
// Modify the local copy of the value.
number = number + 50;
// Display the modified value.
System.out.println("Value Inside Method (After Modification) : " + number);
}
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create an object of the current class.
CallByValue object = new CallByValue();
// Declare and initialize a variable.
int number = 100;
// Display the value before calling the method.
System.out.println("Value Before Method Call : " + number);
// Call the method by passing the variable.
object.modifyValue(number);
// Display the value after the method call.
System.out.println("Value After Method Call : " + number);
// Example Output:
// Value Before Method Call : 100
// Value Inside Method (Before Modification): 100
// Value Inside Method (After Modification) : 150
// Value After Method Call : 100
}
}