Why ‘int’ – integer data type sizes are fixed in Java ?

Let us first understand what happens in C programming language and C++ programming language. In C and C++, the sizes of data types such as ‘int’ and ‘long’ depend on the target platform. On a 16-bit processor such as the 8086, integers are 2 bytes, but on a 32-bit processor like a Pentium or SPARC they are 4-bytes. Similarly, ‘long’ values are 4-byte on 32-bit processors and 8-byte on 64-bit processors.

In Java, the sizes of all numeric types are platform-independent. Note that Java does not have any unsigned types.

In Java Programming Language, the range of the integer type do not depend on the machine on which you will be running the Java source code. This takes away a major pain for the programmer who wants to move software from one platform to another, or even between operating systems on the same platform. Whereas, C and C++ programs use the most efficient integer type for each processor. As a result, a C/C++ program that runs perfectly fine on a 32-bit processor may exhibit an integer overflow on a 16-bit computer. As we know Java is compile-once-run-anywhere, Java programs must run with the same results on all machines, the ranges for the various types are fixed.


About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java and Linux Programming. 

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.