Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

In Java, size doesn't matter

Can you ascertain the size of a primitive in Java?

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

QIs there a method in Java for determining the size of a primitive type, similar to C++'s sizeof operator?

A The short answer: you can't find the size of a Java primitive type through a sizeof operator. Instead, all primitive types have a fixed size. Java was designed in this way to facilitate portability. Because Java specifies the primitive size up front, you can write a Java program and run it reliably on any JVM. If the designers had chosen to not define the size of primitive types, a program that ran on one hardware architecture could fail on another.

Another reason for a lack of sizeof: Java handles all memory access for you. As such, there is no need to find the sizeof some primitive or object in order to allocate a space of memory for it.

Nonetheless, times do arise when you will need to know the size of a primitive. For those times, I give you the following table:

Type Size
boolean 1 bit
char 16 bit
byte 8 bit
short 16 bit
int 32 bit
long 64 bit
float 32 bit
double 64 bit
The sizes of the various Java primitives


  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources