• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java integers three representations

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic