Ashwin Gupta
Ashwin Gupta

Reputation: 2197

What is the double/int value equivalent to null?

For Strings I can do String s = null since String is an object in java. For chars I can do char c = '\u0000'; because that is a "null" character (I could also use blank space character). My question is, for int/double primative types what is the equivalent to null (if any)? I don't want to use 0 because in the event that one of my values is actually 0 this won't work.

Note: I need this because I'm parsing and tokenizing from a file and if the parsing fails to parse an int/double it needs to set its return value to something for its invoking class to check.

Upvotes: 1

Views: 8136

Answers (2)

John3136
John3136

Reputation: 29266

Application defined.

0 may work for some applications. Any negative number may work for others. For doubles/floats you could use NaN (aka Not a number) via double x = Double.NaN;

Upvotes: 3

pczeus
pczeus

Reputation: 7867

There is not actual 'null' equivalent. Typically I have used or have seen -1 used to represent an invalid/null value. If of course you don't expect negative numbers.

Upvotes: 1

Related Questions