This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java integers three representations  RSS feed

 
Charlie James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charlie James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!