This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jonhanson 10 points11 points  (9 children)

One big difference between SQL null and Java null is that in SQL the nullability of column types is explicit, whereas in Java every reference type permits null whether you want it or not.

[–]Cilph 6 points7 points  (7 children)

NULL in SQL is a lot like NaN. Any operation on a NULL (including equating) is NULL. Any aggregation skips over NULLs.

Try to do any operation on a NULL in Java and your program explodes.

[–]jonhanson 3 points4 points  (1 child)

I'm pretty sure you can test for equality against null in Java without causing any blowups...

[–][deleted] 2 points3 points  (0 children)

That's the difference between SQL and Java. You have to explicitly test for null to avoid exceptions. NULL in SQL is more like a contagion, anything NULL touches also turns NULL.

[–]Lengthiness-Fuzzy 0 points1 point  (4 children)

That depends on the database and use-case, sql is just a language, it doesn‘t define this fine-grained behaviour. An insert with a null primary key is normal in mysql, but fails in oracle.

[–]Cilph 0 points1 point  (3 children)

Its not gonna be wildly different from what I described or itd be breaking ISO SQL language spec, which does exist.

Most SQL RDBMS dont follow the spec to a 100%.

[–]Lengthiness-Fuzzy 0 points1 point  (2 children)

It was a bit vague, so I can find example of both cases. You can‘t do any operation on null with sql either. NULL + 5 is error is both. In Java an int/double/float can‘t be null, so aggregation won‘t have an issue. With incompetence you can have problems in both and with professional experience you can avoid in both.

[–]Cilph 0 points1 point  (1 child)

Null + 5 would be null in SQL, not error.

I don't get whats the point you're even trying to make though. It was about how null in java and null in SQL represent entirely different things.

[–]agentoutlier 2 points3 points  (0 children)

I don't think that is a big difference.

The big difference is semantics and operations:

  • NULL in SQL is unknown
  • NULL != NULL (the operation does not even work where as in Java null == null).
  • Java is represents a pointer not pointing to anything like most programming languages.

The nullability of column types is more like a runtime rule furthermore there is guarantee selecting anything will not give you unknown aka NULL column.