File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion byte->int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion byte->int" Watch "Conversion byte->int" New topic

Conversion byte->int

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Why is byte 017 different from byte 17?

C:\Java\EigeneJavaProgramme>java Question02b
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This a bit of java that crept over from C.
Any integral literal that begins with 0 (zero) is taken to be in base 8. So 017 (octal) is 1*8+7 or 15 (decimal). Should have been left behind if you ask me.
You might get a shock if you held a telphone number in a int or long. 017702345 ain't going to be what
you get.
[ August 31, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
And while we're at it, byte value 0x17 means 17 in hex = 16+7 = 23.
This is also a carryover from C, but much more useful than octal.
[ August 31, 2002: Message edited by: Ron Newman ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Unless you still have yer PDP 11!
I agree. Here's the link:
subject: Conversion byte->int
jQuery in Action, 3rd edition