-5

I keep doing classes and typing away at learning programming and in a few different lesson they say this is how you declare a null variable(java and Kotlin if i remember right python is a "none" not exactly the same).

But everything I read or learn says don't do it. Why do it or is there a legitimate use for one?

REV 1.0 Below

I edited and rolled this question back some because a none after some more digging into the subject acts very similar as a null.

Instead of changing my question please make comments on how i can look at it differently or reframe or ask better questions.

  • 1
    Cause `if (root == null) { /* do cool stuff */ }`. – ChiefTwoPencils Mar 13 '18 at 02:14
  • `null` is a way to assign *nothing* to a variable. It can be used for default values or if something is just missing. Obviously it's always dangerous if you deal with variables that might be `null`, since they don't reference any object. I mean, if you have a method like `buyApple()` that it is supposed to return an `Apple`, what do you do if the supermarket has closed already and there is no apple? You may return `null` (in that specific case `Optional` might be better though). Your program logic then obviously needs to handle this case. If you miss that, you get exceptions. – Zabuzard Mar 13 '18 at 02:15

2 Answers2

2

There are multiple legitimate uses of null; although there are equally many (if not more) illegitimate ones.

As a couple which spring to mind:

Optional dependencies. In the below class; if a non-null Notifier is provided at construction; then this is used to be notified when things happen. But the class can still do it's doThing method if there was no such Notifier available (or required).

class ThingDoer {
    private final Notifier notifier;

    ThingDoer(final Notifier notifier) {
        this.notifier = notifier;
    }

    void doThing() {
        // Doing some stuff.
        if (this.notifier != null) {
            this.notifier.notify("Did thing");
        }
    }
}

Caching is another case (use the cached value if it's already been calculated, otherwise go find it from scratch - thread safety not considered here...). The same concept applies to lazily-loaded things; where you can keep the value as null until it's first requested, and then keep it available to avoid recomputation on subsequent requests for it.

class CachedLookup {
    private Result cachedResult = null;

    Result getResult() {
        if (this.cachedResult != null) {
            return this.cachedResult;
        }

        this.cachedResult = this.getResultInternal();
        return this.cachedResult;
    }

    private Result getResultInternal() {
        // Whatever complex stuff here...
        return new Result();
    }
}
BeUndead
  • 3,463
  • 2
  • 17
  • 21
1

For declaring a variable in an outer scope, for example any Object field in Java that isn't explicitly assigned at declaration is null by default

Also derefencing something to allow garbage collection

Its recommended to avoid nulls because chasing down NPE errors is a big time sink in JVM development

Personally, I like Scalas model of Try/Success or Option/Some/None in addition to Nil

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245