in java if a program sees a number like 000 does java interpret it as 0 or does it interpret it as 000? I notice that my calculator wont even let me input 000 so it leads me to wonder if java calculates numbers in this way as well
4 Answers
This question shows (to me) that you have a fundamental misconception about the way that Java works, and possibly a fundamental misconception about integers in the mathematical sense.
Lets start with this excerpt:
"... does java interpret it as 0 or does it interpret it as 000"
This implies to me that you think there is a difference between "0" and "000" in a mathematical sense. Plainly, there is no such difference. Both are textual representations of the Integer zero. The first form is the conventional representation, and the second one is an unconventional representation.
In short, the excerpt quoted above has no meaning in a mathematical sense.
Now in Java, the int type is most widely used representation type for mathematical integers. And the int type has one and only one representation for zero.
In short, the excerpt quoted above has no meaning if we are talking about Java int values in the computational / behavioral sense.
In Java source code, integer literals are written as a sequence of decimal digit characters; e.g. 0, 1, 42 and so on. In this context (and this context only!), an integer literal that starts with a zero is parsed as an octal number; i.e. base 8. So in Java source 012 actually means the number ten. However 000 interpreted as an octal number is still zero.
In short, the excerpt quoted above has no meaning if we are talking about literals in Java source code.
Finally, we need to deal with the situation where a Java application has to turn a sequence of digits entered by a user into a Java int value. In this context, the conversion is typically done by calling Integer.parseInt(...) or something similar. There are two distinct versions of this method:
Integer.parseInt(string)expects the string to be a sequence of decimal digits (with an optional sign). This will return theintzero value for both"0"and"000". Any leading0characters are simply ignored.Integer.parseInt(string, radix)expects the string to be a sequence of digits (with an optional sign) whose base is given by theradixargument. Once again, leading zeros are ignored.
In short, the leading zeros are ignored, and therefore "0" and "000" will be parsed as the int zero, and the distinction in the quoted excerpt does not exist.
0is a valid number. If you're using "000", it's a string, and if you convert it to a number it becomes `0'.0can be expressed as0or00or00000000000-- all valid representations of the exact same value. The CHARACTER STRING"0", however, is different from the CHARACTER STRING"00"or"0000000".000is a perfectly valid number.