hi i need the help that how to calculate the bytes of datatypes in java...?
if it in c/c++ we will use sizeof.. but in java.. how could?

Recommended Answers

All 4 Replies

The Java language specification defines the behaviour of the primitive data types - eg byte is an 8 bit value with values from -128 to 127, inclusive, but there is no specification for how these values are actually stored by the Java Virtual Machine, although you can guess. There is nothing in the spec for the memory size of Objects.
Given that Java handles all memory management for programs, there is no practical use for sizeof.
Why exactly do you want to know?

when u converting to byte, each datatype has oven size, they are not same...
but you can dynamically define the size of byte when creating an array

In C language we studied as 2 byte for integer, even if we dont know the bytes by using sizeof we can calculate integer byte{ex. int a;printf("%d",sizeof(a)); ans is 2}. so like wise i m asking? help me?

The Java language specification defines the behaviour of the primitive data types - eg byte is an 8 bit value with values from -128 to 127, inclusive, but there is no specification for how these values are actually stored by the Java Virtual Machine, although you can guess. There is nothing in the spec for the memory size of Objects.
Given that Java handles all memory management for programs, there is no practical use for sizeof.
Why exactly do you want to know?

Like I said - the various data types are defined by the range of values they can hold. Nowhere does it specify how much memory they will use in the Java VM, but you can guess. eg, the Java Language Specification says:

The values of the integral types are integers in the following ranges:

For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807, inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

That corresponds to:
byte = 1 byte
short and char = 2 bytes
int = 4 bytes
long = 8 bytes

You can do the same for the floating point types. The boolean type is just defined as having two value (true and false) with no clues at all about how that is stored.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.