Edmund Rojas
Edmund Rojas

Reputation: 6606

how does java interpret a number like 000?

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

Upvotes: 0

Views: 5830

Answers (4)

Stephen C
Stephen C

Reputation: 719709

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 the int zero value for both "0" and "000". Any leading 0 characters 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 the radix argument. 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.

Upvotes: 8

csarathe
csarathe

Reputation: 430

Java will interpret as 0 though it is a octal evaluation. Try out

int x=000; //result will be 0

and then

int x=012; // result will be 10 

Upvotes: 2

Luis
Luis

Reputation: 1294

If you enter this in a field and parse it, it is interpreted as 0. If you enter it as a literal in your code, it is an octal 0. Using octals in the code is not a great idea since it is extremely simple to confuse them.

Upvotes: 5

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

In Java any literal integer that starts with a 0 is interpreted as octal. So this is octal 0 which is of course dec 0.

Upvotes: 3

Related Questions