Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Java integers three representations

Charlie James
Greenhorn

Joined: Oct 30, 2005
Posts: 3
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 ?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 30, 2005
Posts: 3
Thanks for that clarification, I did a copy and paste to quickly add 3 more variables d, e, f with different assignments. Then in the print statement didn't change the + a), + b), + c) output part: thinking there all the same, ahh da! lol...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 stored as bits...

byte (8 bits):
00011100

short (16 bits):
0000000000011100

int (32 bits):
00000000000000000000000000011100

long (64 bits):
0000000000000000000000000000000000000000000000000000000000011100

Consider Paul's rocket mass heater.

subject: Java integers three representations