diff options
Diffstat (limited to 'chapter13')
| -rw-r--r-- | chapter13/README.md | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/chapter13/README.md b/chapter13/README.md index 49737663..892f7170 100644 --- a/chapter13/README.md +++ b/chapter13/README.md @@ -57,10 +57,30 @@ struct Person { FamilyTree tree?; // A family tree, or not } struct FamilyTree { - Person father?; + Person father?; // FamilyTree can reference Person Person mother?; Person []kids?; } ``` -Normal field syntax works: `person.tree.father = new Person;` or `person.tree.father.name = "Dad";`. +Normal field syntax works: +e.g. `person.tree.father = new Person;` or + `person.tree.father.name = "Dad";`. + +Reference fields all start out null, and can be tested as such. However, a +non-null reference field can only be stored with a non-null ref. + +```java +struct N { N next; int i; } +N n = new N; +return n.next; // Value reports as null, despite field typed as not-null +``` + +And: + +```java +struct N { N next; int i; } +N n = new N; +n.next = null; // <<-- Compile error, cannot store null into not-null field +return n.next; +``` |
