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

# integral literals? 0xff

Duncan Allen
Greenhorn

Joined: Jan 15, 2002
Posts: 19
Hi,
I am having difficulty understanding why the values represented in these statements are what they are
int b=0xff
int i= 0x7FFF
When I compile and run i get 255 and 2047.
Is there an easy way to understand what and how these numbers represent?
There was a great posting a little while ago explaining Octal and Hex numbers, and I am clear on their values.
I am scheduled to write the exam a week from today and would like to iron out these bugs in my brain. Thanks for your help.
p.s. Should I understand how to do these calculations. I see them occasionally on mock exams.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

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

Joined: Dec 20, 2001
Posts: 3271

int b=0xff
int i= 0x7FFF
When I compile and run i get 255 and 2047.

Val gave some great links, but 0x7FFF is not 2047, it's 32767.
Corey

SCJP Tipline, etc.
Duncan Allen
Greenhorn

Joined: Jan 15, 2002
Posts: 19
Thanx guys,
You are quite right Corey.
typo , one too many f's
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Here's a simple little rule.
1. ALL integral literals are of type int, regardless of their radix (base notation).
Examples:
127 //int literal
456789 //int literal
016 //int literal represented in OCTAL notation
0xFF //int literal represented in HEXADECIMAL notation
0Xffabcd //int literal represented in HEX.
Rule #2:
2. All floating point literals are of type double, casts not withstanding (ie, yes you can include an F or f or (float) and then it won't be a double but I'm just talking about a plain undecorated floating-point literal value)
examples:
1. //double
.1 //double
0.1 //double
123.456 //Can you guess what this is?
789.12 //boolean.
NO THAT WAS A JOKE. IT'S ALSO A DOUBLE.
Hope this makes it crystal clear

Rob
SCJP 1.4

I agree. Here's the link: http://aspose.com/file-tools

subject: integral literals? 0xff