... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2007-01-08

Eclipse Java Compiler Non-Null Warnings

The Eclipse Java compiler does some clever reasoning about the possible null state of variables.  For example, the following code will compile without complaint:

Object result = getMyObject();
result.doSomething();

However, the compiler will generate a warning that result may be null in the following:

Object result = getMyObject();
System.out.println(null == result);
result.doSomething(); // warning: result may be null

The two look the same semantically, don't they?  It would appear that the compiler, as an expediency, presumes that any object returned from a call will be non-null, until it sees evidence that there is any other possibility.  In the second instance, there is such evidence because the programmer has expressed concern explicitly.  A clever trick, to be sure.

Blog Archive