• Post Reply Bookmark Topic Watch Topic
  • New Topic

Type conversion  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

can someone please let me know how the following gives me the according results after execution ? Thanks.

int x = 010;
double y = 2.5;
double erg = x + y;
x++;

x=9
y=2.5
erg=10.5

i expected
x=11
y=2.5
erg=12.5

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what int x = 010; means? It's not the same as int x = 10;

Integer literals that start with an 0 like that are interpreted as octal - base 8. You can also use base 16 (prefix with 0x) or (as of Java 7) base 2 (prefix with 0b).
 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much man. Well understood.
 
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing the following:

and see what you get. Like Matt correctly said, you are considering it 10 while it is actually something else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!