This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes size-of primitive in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "size-of primitive in java" Watch "size-of primitive in java" New topic
Author

size-of primitive in java

Prasanna Santosh
Greenhorn

Joined: Mar 12, 2010
Posts: 5
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

Joined: Apr 14, 2004
Posts: 10336

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)?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prasanna Santosh
Greenhorn

Joined: Mar 12, 2010
Posts: 5
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Mar 12, 2010
Posts: 5
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

That's what they are giving you.
Prasanna Santosh
Greenhorn

Joined: Mar 12, 2010
Posts: 5
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: size-of primitive in java
 
Similar Threads
Size of an Object
How to convert java application to windows exe
refrance variable clearance required..
Array length Compared to C++
Alternative of sizeof method in Java