sizeof() Function in Java

print size in byte of various variable types (c/c++, m/mm, java)

History

With 8 and 16 bit machines, an int or integer had two byte (whereas, when Intel defined it, it could be at first just one byte, with a second one added when necessary).

Over the years one of the, well, challenging areas in software development on different hardware platforms is the use of the right variable, when there are different types.

Starting from zero (0), positive values increased the binary value to a certain maximum, negative values were subtracted; so a „+1“ would be represented as x0001 and a „–1“ as xffff, and so on. The maximum value in the „middle“ at x7fff switching from a +max to a –max..

The variable with type Byte is always one byte = 8 bit.

So far so good.

But things became a bit fuzzy with introduction of 32 bit and later the 64 bit machines.

Depending on the compiler and the operating system’s architecture (16 bit vs 32 bit for a 32 bit hardware platform) int or integer types could be two or four byte long. So there was need to somewhat control the variable size; and short and long were introduced.

The variable type short is two byte = 16 bit.

While int can vary in size two or four byte, long is four byte = 32 bit.

Then 64 bit machines became popular. Here, depending on the compiler and the operating system’s architecture (32 bit vs 64 bit for a 64 bit hardware platform) int or integer were now always four byte long, but now long could be either four or eight byte. To regain control, the type long long was introduced as always eight byte.

To summarize, long can be either four or eight byte, long long is eight byte = 64 bit.

Problem and Solution

As it is often unknown on what platform the developped application will run (64 bit vs 32 bit vs 16 bit) it is essential that a chosen variable type is of the planned or designed size!

With c/c++ the function sizeof() is avalaiable and returns that data in byte.

Java, unfortunately, does not provide such tool. And a (blind) trust that the size of a given variable is as specified can be fatal when, say, the variable declarations are changed or unknown and applications then end up causing unpredictable results or faults.

I have, therefore, written a few methods in Java to return the size of given variable types. After all, the compiler should know; and not having a Java type sizeof() only means, well, write your own! 😉

Particular when calling the c/c++ snippet in iOS and (through JNI) in Android you can compare the size of variable types when run on different devices: long in c/c++ is four byte on iOS <= 9 (= 32 bit environment) and eight byte on iOS >= 10 (= 64 bit environment). Likewise for Android: Nexus 7 with Android 6 shows long with sizeof = 4 and Xiaomi M1 with Android 9 shows sizeof = 8.

I created a class in Java with methods „sizeof()“ in various variable types that are then simply called within a the Java program, passing the variable and returning its size in byte. You are welcome to browse my Java code snippets. (see github)


Enjoy,
    jm.

San José, California
16-June-2019

Computers help you solve problems you would not have without them!

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.