• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

size-of primitive in java

 
Prasanna Santosh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was wondering how to know the size of primitives in java programatically? i know it has fixed values but still is there any way to know that like sizeof in C/C++?
Thanks for your Help

Sano
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. Java is designed so you don't have to care about these sorts of things. Any reason you want to do this (given you already know the size of primitives)?
 
Prasanna Santosh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply, Ya , i was teaching some one java and one person suddenly asked me the question. hence just to clarify the concepts. (say a written proff) kindof

Thanks Sano
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out some of the constants in the wrapper classes. For instance Float.SIZE, which holds "The number of bits used to represent a float value." (taken from the Javadocs).

They also have other useful constants, for things like the minumum and maximum values that can be stored in the primitive types.
 
Prasanna Santosh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brown, Actually i tired the same for Integer and Float it gives 32 bytes. but actually i am trying to look for the size of primitive types like int,float etc

Thanks Sano
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what they are giving you.
 
Prasanna Santosh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya got the point. got confused a little bit about the sizes. Sorry

Thanks paul and Brown

Thanks Sano
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Santosh wrote:Thanks Brown, Actually i tired the same for Integer and Float it gives 32 bytes. but actually i am trying to look for the size of primitive types like int,float etc

That's 32 bits, not bytes.
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For primitive data types in bits:

booleans not defined... they are extremely small
byte 8
short 16
char 16
int 32
long 64
float 32
double 64
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are the sizes of primitive variable types in bits as they will look like from inside your Java program. For a boolean, in principle only one bit is needed.

But note that a particular JVM implementation might use more memory for primitive types on the underlying platform. For example, on some processors working with 32-bit integers is more efficient than with 8-bit bytes, so the JVM will actually use 32 bits for a variable of type byte. In your Java program however, you will not notice this - there, a variable of type byte is always 8 bits, regardless of how the JVM represents it on the underlying platform.

There is no (easy) way to find out in a Java program how much memory is actually used for a primitive type of a certain size - those things are implementation details of the JVM that your program is running on, which you're not supposed to be concerned with from inside your program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic