-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes
More file actions
92 lines (72 loc) · 3.7 KB
/
Copy pathnotes
File metadata and controls
92 lines (72 loc) · 3.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
1. final and finally
final can be used to make a variable "unchangable"
private final String name = "foo"; // the reference name can never change
final can also make a method not "overrideable"
public final String toString() { return "NULL";}
final can also make a class not "inheritable". i.e. the class cannot be subclassed
public final class finalClass {...}
public class classNotAllowed extends finalClass {...} // Not allowed
finally
finally is used in a try/catch statement to execute code "always"
lock.lock()
try {
// do stuff
} catch (SomeException se) {
// handle se
} finally {
lock.unlock(); // always executed, even if Exception or Error or se
}
2. redirect to /dev/null
redirect stdout to /dev/null, i.e. discard/silent the output by command
3. can cast child instance to parent type, and cast back, NO member loss!
public class Phone {
private boolean has3g;
public boolean has3g() {
return has3g;
}
public void setHas3g(boolean newVal) {
has3g = newVal;
}
}
public class Blackberry extends Phone {
private boolean hasKeyboard;
public boolean hasKeyboard() {
return hasKeyboard;
}
public void setHasKeyboard(boolean newVal) {
hasKeyboard = newVal;
}
}
If I was to create an instance of Blackberry, cast it to a Phone object and then cast it back to Blackberry, would the original Blackberry object lose its member variables? E.g:
Blackbery blackbery = new Blackberry();
blackbery.setHasKeyboard(true);
Phone phone = (Phone)blackbery;
Blackberry blackberry2 = (Blackberry)phone;
// would blackberry2 still contain its original hasKeyboard value?
boolean hasKeyBoard = blackberry2.hasKeyboard();
Q: If I was to create an instance of Blackberry, cast it to a Phone object and then cast it back to Blackberry, would the original Blackberry object lose its member variables?
A: You have instantiated a Blackberry. This will remain a Blackberry until the it is GCed.
When you cast it to Phone you are not changing the fact that the type is Blackberry.
You are just treating it as a Phone i.e. you have only access to its generic properties (that of Phone).
The extended properties of Blackberry are no longer visible despite the fact that the concrete instance is still a Blackberry
and you can successfully cast it back to access the Blackberry properties.
4.Java is always pass-by-value.
Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.
It goes like this:
public static void main(String[] args) {
Dog aDog = new Dog("Max");
// we pass the object to foo
foo(aDog);
// aDog variable is still pointing to the "Max" dog when foo(...) returns
aDog.getName().equals("Max"); // true
aDog.getName().equals("Fifi"); // false
}
public static void foo(Dog d) {
d.getName().equals("Max"); // true
// change d inside of foo() to point to a new Dog instance "Fifi"
d = new Dog("Fifi");
d.getName().equals("Fifi"); // true
}
In the example above aDog.getName() will still return "Max".
The value aDog within main is not changed in the function foo with the Dog "Fifi" as the object reference is passed by value.
If it were passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.