Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java has fixed data type size unlike C

In C as we know the size of data types (ex. int) can vary depending on compiler / hardware.

But why the size of data types is constant in java language? Why don't we have the flexibility for different data type size in java depending on compiler?

like image 620
Harish Avatar asked Dec 05 '09 14:12

Harish


People also ask

Why does Java have different data types?

Java is statically typed and also a strongly typed language because, in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data ...

Why the size of all data types in Java on all platforms are same?

The idea of java was "Write once, Run anywhere" without recompiling. That means every VM has the same data size. Of course, on 64 bit machines, it uses 64 bit references, but you don't have access to those so it doesn't matter.


6 Answers

The JVM (Java Virtual Machine) is designed to be platform independent. If data type sizes were different across platforms, then cross-platform consistency is sacrificed.

The JVM isolates the program from the underlying OS and platform. This can make life difficult for performing system-specific work, but the benefits are that you can write-once, run-anywhere (this is largely true, with some unfortunate issues. Write-once, test-everywhere is a much more practical approach).

like image 120
Brian Agnew Avatar answered Oct 05 '22 05:10

Brian Agnew


If data type size varies on different platforms you lose portability.

like image 39
Buhb Avatar answered Oct 05 '22 05:10

Buhb


To get a really comprehensive answer to this, you'd need to do a great deal of historical reading from the early days of Java. Certainly, the designers could have included a more complicated primitive type system. However, when Java burst onto the broad stage, it was aimed at applets. Code to run in a browser, organizing complex UI, didn't (and doesn't) need to know whether it is running on the infamous MNS-49 (7 7-bit chars per word), or the Honeywell 68000 (4 9-bit chars per word), or a boring modern processor. It's much more important than anyone can code bit arithmetic on an int and know what's going to happen after 32 shifts.

like image 43
bmargulies Avatar answered Oct 05 '22 05:10

bmargulies


The flexibility of C for this has some advantages (reduced memory/storage consumption if you use 32 instead of 64 bits), but these advantages tend to become less relevant as the hardware improves (this was designed in the 70s).

This flexibility however comes with severe interoperability and long-term vision problems (Y 2038 bugs).

In contrast, a Java object has anyway some storage overhead, so saving 4 bytes on each Date object would be quite pointless and only troublesome.

like image 38
Zorglub Avatar answered Oct 05 '22 07:10

Zorglub


Because that's Java. See the Java language specification.

like image 26
michael.kebe Avatar answered Oct 05 '22 05:10

michael.kebe


The idea of java was "Write once, Run anywhere" without recompiling. That means every VM has the same data size. Of course, on 64 bit machines, it uses 64 bit references, but you don't have access to those so it doesn't matter.

It works pretty well, but one thing I do wish is that wish we could get 64 bit array indexes. This didn't really matter back in the day, but for large memory mapped files it's a huge pain. You have to break them up into 2gb chunks.

like image 30
Chad Okere Avatar answered Oct 05 '22 05:10

Chad Okere