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
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
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
inttype is most widely used representation type for mathematical integers. And theinttype has one and only one representation for zero.In short, the excerpt quoted above has no meaning if we are talking about Java
intvalues in the computational / behavioral sense.In Java source code, integer literals are written as a sequence of decimal digit characters; e.g.
0,1,42and 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 source012actually means the number ten. However000interpreted 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
intvalue. In this context, the conversion is typically done by callingInteger.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 theintzero, and the distinction in the quoted excerpt does not exist.