3

I have encountered the most weird problem ever in Java.

When I am doing this:

int []s = new int [5];
s[0] = 026;
s[1] = 0011;
s[2] = 1001;
s[3] = 0026;
s[4] = 1101;

The numbers being stored in the array, seen from debug mode, are different i.e. the numbers stored are 22,9,1001,22,1101. Can you give me some hints what might be wrong with this?

1
  • Four identical answer... Commented Feb 24, 2011 at 17:15

4 Answers 4

7

Integer literals that start with 0 are interpreted as being in octal format.

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

1 Comment

Slightly more formally: integer literals that start with 0 are interpreted as being in octal format.
4

Literal bytes, shorts, integers and longs starting with 0 are interpreted as being octal

The integral types (byte, short, int, and long) can be expressed using decimal, octal, or hexadecimal number systems. Decimal is the number system you already use every day; it's based on 10 digits, numbered 0 through 9. The octal number system is base 8, consisting of the digits 0 through 7. The hexadecimal system is base 16, whose digits are the numbers 0 through 9 and the letters A through F. For general-purpose programming, the decimal system is likely to be the only number system you'll ever use. However, if you need octal or hexadecimal, the following example shows the correct syntax. The prefix 0 indicates octal, whereas 0x indicates hexadecimal.

from Primitive Data Types

Comments

2

The numbers with leading zeros are read as octal.

Comments

0

Numbers with leading zeros are termed as octal.

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.