diff options
| author | Cliff Click <cliffc@acm.org> | 2024-08-05 13:55:46 -0700 |
|---|---|---|
| committer | Dibyendu Majumdar <dibyendumajumdar@users.noreply.github.com> | 2024-08-06 00:50:58 +0100 |
| commit | 702b696cc74a20cf84172ee2ce59ace4c980ee75 (patch) | |
| tree | f02b4869a3a094f7c9cfcf8ee098cca351006f61 /chapter13/README.md | |
| parent | e29c22656458e9252ca0fc32a396d605496b4b4d (diff) | |
| download | Simple-702b696cc74a20cf84172ee2ce59ace4c980ee75.tar.gz Simple-702b696cc74a20cf84172ee2ce59ace4c980ee75.zip | |
Update README.md
Diffstat (limited to 'chapter13/README.md')
| -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; +``` |
