• Post Reply Bookmark Topic Watch Topic
  • New Topic

Leading Zero Causing Unexpected Results  RSS feed

 
Raymond Van Eperen
Greenhorn
Posts: 17
Java Mac Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an array of values for a TestNG Data Provider, and ran into some strange behavior I didn't expect. When putting theint value 03451 into an Object array, it stores it as 1833. It appears that it might be using Integer.valueOf() to box it into an Integer object to put in the array, but I expected the resulting value to be 3451, not 1833. Can anyone explain? Here is the code snippet:

From Debugger:


Code where I saw the problem. The first element is the value being tested and the second is the expected result when changed to a String:
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a leading zero in an integer literal value you are defining it as a number in Octal (values 0-7), rather than Decimal (0-9). So 03451 is not a decimal number equivalent to 3451, it is an octal value equivalent to the decimal value of 1833.

Here is a link to the Tutorial: Primitive Data Types
 
Raymond Van Eperen
Greenhorn
Posts: 17
Java Mac Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doooh!!! I should have known that.

Thanks Steve
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!