Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative of sizeof method in Java

 
Rajesh Kesarwani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

Can anybody tell me that, what is the alternative of sizeof method as in c/c++, to find out the size of primitive datatypes in Java.
:roll:

Actually, i want to know, how many bytes r taken by boolean type in java.

Thanks in Advance.
 
Anil Kumar Saha
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this

sizeOfInJava
 
Rajesh Kesarwani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have already gone through this link.
But doesn't understand what it really want to say.
Pls tell me more precisely.
Particularly, size of boolean type.
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sizes for the data types are in the Java Tutorial, here. The size of a boolean is presumably one bit because the value of a boolean can only be true or false. How that value is implemented in the VM can be something different. I would assume that the amount of memory displaced by a boolean would be a word of memory on the native system (4 bytes on most PC OS's).
 
Rajesh Kesarwani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But is there any way to find out, Boolean type takes 1 bit.

Pls support ur answer.
 
ak pillai
author
Ranch Hand
Posts: 288
 
Rajesh Kesarwani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this link size of boolean is specified as true or false.what does it mean.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Kesarwani:
But is there any way to find out, Boolean type takes 1 bit.


No. The size of a boolean value is an implementation detail of a particular JVM. When you're talking about local variables, they're actually going to be a full machine word. When you're talking about member variables or arrays, it's up to the implementation whether a single byte is used or something larger for padding. There's no way to know other than looking at the source code for a particular JVM.

Read the article.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the required ranges, it's possible to infer that (for example) a boolean takes a minimum of one bit, or a short takes a minimum of two bytes. But it's entirely possible that, for example, a 64-bit machine might just use 64 bits for every primitive type, just because it's convenient. The javaworld article gives suggestions how you can measure this sort of thing experimentally, but be aware that whatever you learn, it may be completely different on a different machine, or a different JVM version.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic