Why Java does not have sizeof operator like C&C++?


In C and C++, the sizeof( ) operator satisfies a specific need as it tells you the number of bytes allocated for data items. The most compelling need for sizeof( ) in C and C++ is portability.

Different data types might be different sizes on different machines, so the programmer must find out how big those types are when performing operations that are sensitive to size.

For example, one computer might store integers in 32 bits, whereas another might store integers as 16 bits. Programs could store larger values in integers on the first machine. As you might imagine, portability is a huge headache for C and C++ programmers.

Java does not need a sizeof() operator for this purpose, because all the data types are the same size on all machines. You do not need to think about portability on this level. it is designed into the Java language.