# A problem with java literals(Hex & octal values etc)

Femi Alla

Ranch Hand

Posts: 79

posted 13 years ago

Hello, I would be sitting for the programmer exam on saturday and there is only one set of questions I would skip 'cos I don't know. It's

a little problem with the declaration of octal and hexadecimal values in java.

The Java 2 certification study guide says on page 11 that

"The value of 28 may thus be expressed six ways:

28// Decimal value.

034//this is supposed to be in base 8,right?

0x1c}

0x1C} Hexadecimal values.

0X1c}

0X1C}

No matter how hard I've tried, I don't seem to understand any except for the first. My calculation of 28 to base 8(the second item on the list) is: 2x81 + 2x80

Which would give; 16 + 2. I'd have expected the result to be 018. But it's 034. The rest I don't even know what to think.

I need help here. Can anyone please help me out?

Thanks.

a little problem with the declaration of octal and hexadecimal values in java.

The Java 2 certification study guide says on page 11 that

"The value of 28 may thus be expressed six ways:

28// Decimal value.

034//this is supposed to be in base 8,right?

0x1c}

0x1C} Hexadecimal values.

0X1c}

0X1C}

No matter how hard I've tried, I don't seem to understand any except for the first. My calculation of 28 to base 8(the second item on the list) is: 2x81 + 2x80

Which would give; 16 + 2. I'd have expected the result to be 018. But it's 034. The rest I don't even know what to think.

I need help here. Can anyone please help me out?

Thanks.

SCJP

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 13 years ago

You've got a few things turned around. It's 34 which is already in octal - so to see the equivalent decimal value, calculate

3 * 8^1 + 4 * 8^0 = 24 + 4 = 28

(where of course ^ is "to the power of", not the java OR operator) Likewise 0x1C is in hex, so convert to decimal thus:

1 * 16^1 + 12 * 16^0 = 16 + 12 = 28

3 * 8^1 + 4 * 8^0 = 24 + 4 = 28

(where of course ^ is "to the power of", not the java OR operator) Likewise 0x1C is in hex, so convert to decimal thus:

1 * 16^1 + 12 * 16^0 = 16 + 12 = 28

"I'm not back." - Bill Harding, *Twister*