6

I was working with one problem and came across this. What happen is:

when we use this: BigInteger.valueOf(10000) it gives value of 10000

But

when we use this BigInteger.valueOf(0010000) it gives value of 4096

Whats the difference between the two?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
murtaza.webdev
  • 3,523
  • 4
  • 22
  • 32
  • See: http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1 –  Oct 24 '16 at 06:50
  • 4
    Possible duplicate of [Why is 08 not a valid integer literal in Java?](http://stackoverflow.com/questions/7218760/why-is-08-not-a-valid-integer-literal-in-java) – Erwin Bolwidt Oct 24 '16 at 06:54
  • @ErwinBolwidt : this example will help user in understanding of possible scenario. as it happen with me. – murtaza.webdev Oct 24 '16 at 08:44

3 Answers3

12

0010000 is an octal literal. This has nothing to do with BigInteger - it's just Java integer literals (JLS 3.10.1):

System.out.println(10000);   // 10000
System.out.println(0010000); // 4096

From the JLS:

A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9 interspersed with underscores, representing a positive integer.

...

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

The second one is integer in octal system, the first in decimal, that is the reason of the difference

holmicz
  • 577
  • 4
  • 15
2

this is taking decimal literal as parameter

BigInteger.valueOf(10000) 

and this is taking an octal literal as parameter

BigInteger.valueOf(0010000) because begins with 0

so you are technically passing 2 different numbers

  1. 10000

and

  1. 4096
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97