File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# fundamentals

swarna kumar
Greenhorn

Joined: Nov 11, 2001
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

Joined: Mar 22, 2002
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

Joined: Nov 11, 2001
Posts: 17
thx anthony,now i got it.
swarna
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
how to convert Hexadecimal to decimal?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Ranch Hand

Joined: Jun 26, 2002
Posts: 104