What type of variable that can contain 1,000,000,000(a decimal number) takes the most memory space?
A Java String. Under the hood A Java String consists of an object with 3 fields, one of which points to a separate array object containing the characters. Plus of course, Java Strings are composed of 16 bit characters1.
If you are worried about memory usage over all other criteria, don't use Java. But for most applications, memory usage is the least of your concerns.
It is worth noting that 1,000,000,000 can be represented using a Java int which will be the same size as a C signed or unsigned (32 bit) integer.
Furthermore, a C int is not necessarily big enough to represent 1,000,000,000. On some platforms, int is 16 bits, and this is allowed by the C standard.
1 - Actually, this is Java platform dependent. For example, in Java 9 they modified the String implementation to use one byte per character for strings that are composed entirely of characters in the range 0 to 255. See this article. But despite this, a Java string still takes more space than a C string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With