What is the difference between null, 0 and nothing?

user3956566 picture user3956566 · Oct 29, 2013 · Viewed 21.8k times · Source

What is the difference between null, 0 and nothing?

I cannot find a question addressing all three of these.

For example:

If I am getting input from a string and parsing it to int.

return Integer.parseInt(sc.nextLine());

Or if I am asking if a string != "", or a is not nothing I am confused about which to use when and why.

I am confused about which one to use when validating data.

Answer

Kevin Bowersox picture Kevin Bowersox · Oct 29, 2013

null means that a variable contains a reference to a space in memory that does not contain an object.

0 is a numeric data type with a value of 0.

Nothing doesn't really exist, however I think you may be viewing this as an empty String "" which is simply a String data type that does not contain a value.

If your looking at this from a Javascript paradigm it may be confusing. In Java variables are not truthy and falsey therefore the type is considered in comparisons.