Charlie James

Greenhorn

Posts: 3

posted 11 years ago

Java integers can have three representations, base-8 (octal), base-10, and base-16 (hexadecimal)?

I am guessing this is short, int, and long. Am I close ?

I am guessing this is short, int, and long. Am I close ?

posted 11 years ago

It's a good guess, but you're mixing two different concepts.

Ultimately, everything is stored as bits (zeros and ones). In Java, byte, short, int, and long are primitive

Different base representations (decimal, octal, and hexadecimal) are available to the Java programmer as different ways to express integral values.

Octal (base 8) literals are prefixed with zero. For example, 034 is an octal representation of 28. Hexadecimal (base 16) literals are prefixed with zero and the letter x, using letters a-f to represent 10-15. (The letters are not case sensitive.) For example, 0x1c is a hexadecimal representation of 28.

Originally posted by Charlie James:

Java integers can have three representations, base-8 (octal), base-10, and base-16 (hexadecimal)? I am guessing this is short, int, and long. Am I close ?

It's a good guess, but you're mixing two different concepts.

Ultimately, everything is stored as bits (zeros and ones). In Java, byte, short, int, and long are primitive

**types**for storing integral values. These types are distinguished by the number of

**bits**they can hold (8, 16, 32, and 64 respectively).

Different base representations (decimal, octal, and hexadecimal) are available to the Java programmer as different ways to express integral values.

Octal (base 8) literals are prefixed with zero. For example, 034 is an octal representation of 28. Hexadecimal (base 16) literals are prefixed with zero and the letter x, using letters a-f to represent 10-15. (The letters are not case sensitive.) For example, 0x1c is a hexadecimal representation of 28.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Charlie James

Greenhorn

Posts: 3

posted 11 years ago

It might also help to visualize this...

As I mentioned, the primitive integral types are distinguished by the number of bits they can hold. This is what is implied by "widening" and "narrowing" type conversions. But whether you enter a value as 28, 034, or 0x1c (decimal, octal, or hexidecimal), it's

byte (8 bits):

00011100

short (16 bits):

0000000000011100

int (32 bits):

00000000000000000000000000011100

long (64 bits):

0000000000000000000000000000000000000000000000000000000000011100

As I mentioned, the primitive integral types are distinguished by the number of bits they can hold. This is what is implied by "widening" and "narrowing" type conversions. But whether you enter a value as 28, 034, or 0x1c (decimal, octal, or hexidecimal), it's

*stored*as bits...byte (8 bits):

00011100

short (16 bits):

0000000000011100

int (32 bits):

00000000000000000000000000011100

long (64 bits):

0000000000000000000000000000000000000000000000000000000000011100

*~Joe Strummer*

sscce.org