programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# fundamentals

swarna kumar
Greenhorn
Posts: 17
hi guys,
examine th code,
class demo{
public static void main(String args[]){
int i=012;
System.out.println(i);
}
}
the result gives 10, why so? and how?,give the full details.
thx
swarna
[LIST]

Anthony Villanueva
Ranch Hand
Posts: 1055
Any integer literal prefixed by 0 is interpreted to be an octal. 012 is 1 x 8^1 + 2 x 8^0 = 8 + 2 = 10.

swarna kumar
Greenhorn
Posts: 17
thx anthony,now i got it.
swarna

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
Java has three types of integer literals:
- decimal: 123, 345, 3764632, ...
- octal: 012, 0345, 076, 012, ...
- hexadecimal: 0xF4E3, 0XABDC, 0x0254, ...
Note that:
- decimal literals are composed of digits from 0 to 9 where the left-most digit cannot be 0.
- octal literals always begin with a 0 followed by digits ranging from 0 to 7
- hexadecimal literals always begin with 0x (zero lowercase x) or 0X (zero uppercase x) followed by digits ranging from 0 to 9 and letters ranging from A to F (upper- or lowercase letters)
[ July 11, 2002: Message edited by: Valentin Crettaz ]

Ranch Hand
Posts: 104
how to convert Hexadecimal to decimal?

Jessica Sant
Sheriff
Posts: 4313
You convert hexidecimal to decimal the same way you convert octal to decimal:
0132 is (1 x 8^2) + (3 x 8^1) + (2 x 8^0) = 64 + 24 + 2 = 90 (octal)
0x132 is (1 x 16^2) + (3 x 16^1) + (2 x 16^0) = 256 + 48 + 2 = 306 (hexidecimal)
[ July 12, 2002: Message edited by: Jessica Sant ]