From 702b696cc74a20cf84172ee2ce59ace4c980ee75 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 5 Aug 2024 13:55:46 -0700 Subject: Update README.md --- chapter13/README.md | 24 ++++++++++++++++++++++-- 1 file 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; +``` -- cgit v1.2.3