2
public static void main (String[] args) { 
    int a = 0010;
    String num = Integer.toString(a);
    System.out.println(num);    
} 

Why does it print 8 not 0010?

Gian
  • 13,735
  • 44
  • 51
kodujuust
  • 35
  • 3
  • Many variations on this question here on SO - see eg http://stackoverflow.com/questions/24031843/is-00-an-integer-or-octal-in-java/24032020#24032020 – fvu Dec 18 '14 at 19:23
  • possible duplicate of [Integer with leading zeroes](http://stackoverflow.com/questions/565634/integer-with-leading-zeroes) – fvu Dec 18 '14 at 19:24

2 Answers2

3

Java interprets integers that start with 0 as being expressed in octal. 10 in octal (base 8) is 8 in decimal (base 10). Integer.toString() is doing the correct thing in this case, because the value of a is indeed 8.

Gian
  • 13,735
  • 44
  • 51
1

Integers are stored in binary form in memory. There are several ways to define integer literal. 0010 is octal, in base 8. toString prints in base 10 (decimal).

Andrey
  • 59,039
  • 12
  • 119
  • 163