Friday, May 13, 2005

java tips for C/C++ coders

datatypes



In C/C++ datatypes are dependant on the system you are compiling for.
This can be 16 bit, 32 bit or 64 bit, and Big Endian or Little Indian.
For 64bit systems it even depends on the data model.
In Java this is all standardised.
An integer is always 32 bit and data is always Big Endian.
Also there are no unsigned types in java, all types are signed, even bytes.
This can be annoying and confusing.
The following table gives an overview for a 32bit i386 and a 64bit AMD64 system:






























































datatypes on a 32bit i386 system
typeJavaC/C++
8bit unsigned-unsigned char
8bit signedbytechar
16bit unsigned-unsigned short
16bit signedshortshort
32bit unsigned-unsigned int
32bit signedintint
64bit unsigned-unsigned long long
64bit signedlonglong long
32bit floating pointfloatfloat
64bit floating pointdoubledouble






























































datatypes on a 64bit AMD64 system
typeJavaC/C++
8bit unsigned-unsigned char
8bit signedbytechar
16bit unsigned-unsigned short
16bit signedshortshort
32bit unsigned-unsigned int
32bit signedintint
64bit unsigned-unsigned long
64bit signedlonglong
32bit floating pointfloatfloat
64bit floating pointdoubledouble



In C and C++, you can use char* and casts to convert simple datatypes into bytes. In java this is not possible. Luckily the java.nio package exists. This contains among things the class ByteBuffer, making data conversions alot easier.



To Be continued...!