Java Reference
In-Depth Information
Memory Address
#000023
Memory Address
#000025
Memory Address
#000029
Test t
int[] t.array
anintArray = new
int[] {100,200,300};
(Memory address
reference is overwritten.)
23
24
25
26
27
28
29
30
31
t object
(general info, such
as length of
following variable
addresses)
array var
=
000025
array[0]
= 1
array[1]
= 2
array[2]
= 3
anintArr
ay var =
000029
anintArr
ay[0] =
100
anintArray
ay[1] =
200
anintArray
ay[2] =
300
figure 4-7  
Note Again, it's important to take special care when dealing with Strings.
Strings are a non‐primitive type, meaning that you might try to write the fol-
lowing code:
class Test {
String a = "a";
static void changeString(String s) {
s = "b";
}
public static void main(String[] args) {
Test t = new Test();
System.out.println("a is: "+t.a);
Test.changeString(t.a);
System.out.println("a is now: "+t.a);
}
}
But see that the changes you make to the String are not reflected in the mem-
ber variable. The reason for this is simple. Remember that assigning (new) val-
ues to a String will always create a new String object. This code is thus equal
to writing:
class Test {
String a = new String("a");
continues
Search WWH ::




Custom Search