0

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

13
  • 3
    If you were to draw 000 on a number line, how would that be any different from 0000 or 0? Since there is no difference, there is no reason for Java to pretend there is, thus it will save it as 0. Commented Jan 21, 2012 at 3:11
  • 1
    There's no such thing as "a number like 000"; it's not a valid number (that's why your calculator won't allow it). 0 is a valid number. If you're using "000", it's a string, and if you convert it to a number it becomes `0'. Commented Jan 21, 2012 at 3:11
  • 1
    Well, Java doesn't have vision, so it doesn't see anything. The NUMBER 0 can be expressed as 0 or 00 or 00000000000 -- all valid representations of the exact same value. The CHARACTER STRING "0", however, is different from the CHARACTER STRING "00" or "0000000". Commented Jan 21, 2012 at 3:12
  • @KenWhite -- 000 is a perfectly valid number. Commented Jan 21, 2012 at 3:13
  • 2
    You could probably try it and see what happens, in less time than it took you to type the question. Commented Jan 21, 2012 at 3:37

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

3

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.

Comments

2

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.