Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 2
    But String is not polymorphic, no? And how do you use the null object pattern for an already existing type like String? Commented Dec 12, 2013 at 9:05
  • String is not polymorphic, no. But even so you would like to know that you can call instance methods on any string reference that you have. This works for all non-null references. String already has a null object: the empty string. Just like lists have the empty list. So ask yourself why the empty string isn't enough in your case. I guess that you use a null string to flag some special case. Ask yourself if you can use a separate flag for that special case. Commented Dec 12, 2013 at 15:27
  • 1
    @AlexanderTorstling: A field of type int defaults to zero rather than null. Conceptually it should be possible to have a string type whose default value would be an empty string rather than null [such a type could semantically be to String what int is to Integer]. The fact that a non-empty string would internally hold a reference to a heap object would be an implementation detail; there's no reason it couldn't behave semantically like a primitive. Commented Dec 23, 2013 at 19:29
  • @supercat: Agreed. I think it would have been better to forbid null values for refefences unless explicitly enabled. Default non-null and final fields. Many types already have null behaviour objects Commented Dec 25, 2013 at 11:32
  • @AlexanderTorstling: Having storage locations of all types default to all-bytes-zero, and providing that structure types can be assigned via all-bytes-copy, greatly simplifies a framework, but pretty much implies that types with mutable reference semantics must default to null. It would have been helpful, however, to have had framework support for value types which would encapsulate a single reference and would "box" as--and could unbox from--that reference type. Commented Dec 25, 2013 at 19:59