aspose file tools*
The moose likes Java in General and the fly likes Alternative of sizeof method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative of sizeof method in Java" Watch "Alternative of sizeof method in Java" New topic
Author

Alternative of sizeof method in Java

Rajesh Kesarwani
Greenhorn

Joined: Apr 04, 2005
Posts: 23
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

Joined: Apr 07, 2004
Posts: 111
Read this

sizeOfInJava


Regards,

Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
Rajesh Kesarwani
Greenhorn

Joined: Apr 04, 2005
Posts: 23
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

Joined: Oct 29, 2001
Posts: 8710
    
    6

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


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rajesh Kesarwani
Greenhorn

Joined: Apr 04, 2005
Posts: 23
But is there any way to find out, Boolean type takes 1 bit.

Pls support ur answer.
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
The sizes are fixed (i.e. constant). We know the sizes from the doco

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Rajesh Kesarwani
Greenhorn

Joined: Apr 04, 2005
Posts: 23
In this link size of boolean is specified as true or false.what does it mean.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
 
subject: Alternative of sizeof method in Java
 
Similar Threads
difference between exe file and batch file
sizeof byte array
[Interview Q] Why does array index begin with 0 instead of 1 ???
memory allocation
data types