-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathE6 Reference Trap.java
More file actions
41 lines (28 loc) · 1.38 KB
/
Copy pathE6 Reference Trap.java
File metadata and controls
41 lines (28 loc) · 1.38 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
import java.text.NumberFormat.Style;
import java.util.Arrays;
import java.util.Scanner;
// Reference Trap
public class HelloWorld {
public static void main(String[] args) {
String[] oldFriends = {"Rahul","Babu","Lucky","Raja"};
String[] newFriends = oldFriends; //it will stores the reference of the oldFriends array.
newFriends[2] = "Srabana"; // if you change the newFriend then oldFriend also change.
System.out.println("Old friends: " + Arrays.toString(oldFriends));
System.out.println("New friends: " + Arrays.toString(newFriends));
// solution
// use loop or Arrays.copyOf();
String[] CopyoldFriends = {"Rahul","Babu","Lucky","Raja"};
String[] newFriendsFixed = Arrays.copyOf(oldFriends, oldFriends.length); // wow trick.
newFriendsFixed[2] = "Srabana";
System.out.println("After Fixed: ");
System.out.println("Old friends: " + Arrays.toString(CopyoldFriends));
System.out.println("New friends: " + Arrays.toString(newFriendsFixed));
}
}
// The state of a varibale should not change because you updated another.
/*
* Trap: Setting array variables equal to each other.
* Pitfall: Both variables point to the same array.
* Solution: Create a new array. Then copy every value using a loop.
* Solution-II: Set it equal to a copy of the array (Arrays.copyOf(array,array.length)).
*/