-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassingObjectsInMethods.java
More file actions
46 lines (36 loc) · 1.45 KB
/
PassingObjectsInMethods.java
File metadata and controls
46 lines (36 loc) · 1.45 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
// Primitives are passed by the values and objects are passed by reference.
// when you pass a primitive value the actual value is copied
//( the method will now operate on the copied value of the primitive , not the original value.)
// when you pass an object the value of the refrence is passed.
//(when you pass the objects in methods you are actually passing the value of refernece of the object not the actual object itself. )
/* code
public class PassingObjectsInMethods { // value will not swap through this method.
public static void main(String[]args){
int a = 2;
int b = 9 ;
swap(a , b );
System.out.println("the value of a is "+ a + " and b is "+b);
}
static void swap(int a , int b){ // the copy of the actual value is passed.
int temp = a ; // the values will not be changed here . because copy of the orignal value value is passedd not the actual value itself
a = b ;
b = temp ;
}
}
*/
/*
code
import java.util.Arrays;
public class PassingObjectsInMethods { // this willl change the oroginal object beacuse
// value of the refrnece is passed and both of them are poinitng to the same object
// in the heap memory (cange made by one variable will be shown in another variable).
public static void main(String[] args) {
int [] arr ={1 , 2 ,3 ,4, 5};
change(arr);
System.out.println(Arrays.toString(arr));
}
static void change (int[]nums){
nums[0] = 99 ;
}
}
*/