I don't understand how an int 63823, takes up less space than a double 1.0. Is there not more information stored in the int, in this particular instance?
-
1C'mon, double and float are floats!, I mean int, short, byte doesn't accept float numbers, system shows the float values in x.xEx format, just set a double as 1000 and print it – Jun 29 '13 at 18:03
-
1The size of the variable reflects the maximum amount of information that can be stored there, not the amount actually stored at any point in time. Look up Shannon and Information Theory. – Hot Licks Jun 29 '13 at 18:34
-
2Ignore the haters. This is a great question. – jason Jun 29 '13 at 18:35
-
@Jason LOL, looking back on this now I feel stupid, but thanks for the respect boss. – Dick Lucas Jul 15 '14 at 14:43
5 Answers
I don't understand how an int 63823, takes up less space than a double 1.0. Is there not more information stored in the int, in this particular instance?
Good question. What you're seeing when you see 63823
and 1.0
is a representation of the underlying data, you are not seeing the underlying data. It is specially formatted so that you can read it, but it is not how the machine sees it.
Java uses very special formats for representing int
and double
. You need to look at those representations to understand why 63823
takes thirty-two bits when represented as a Java int
and 1.0
takes sixty-four bits when represented as a Java double
.
In particular, 63823 as an int
in Java is represented as:
00000000000000001111100101001111
and 1.0 as a double
is represented in Java as:
0011111111110000000000000000000000000000000000000000000000000000
If you want to explore more, I recommend Two's Complement and What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Not exactly. The double 1.0 represents more information because, by the definition of a double as a 64 bit float, there are more values that it could be. To use your example, if you had a special data type that could only have two values, 63823 and 98321234213474932, then it would only take 1 bit to represent the number 63823, though it would be far less useful than an int.
In terms of implementation, it's often a lot easier and faster to work with fixed-size data types, so that you can allocate a fixed chunk of memory (that's what a variable is) without having to know it's value and constantly reallocate space. Examples of a variables with a different approach would be String and BigInteger, which do allocate space to accommodate their values. Note that both are immutable in Java -- that's not a coincidence.

- 15,492
- 2
- 23
- 32
These primitive datatypes need to be defined somewhere for you to use them. It is not a flexible container where you can stuff in whatever you want, rather more like a bottle which takes the same space no matter if full or empty. And they also have a maximum they can contain. Read more yourself here.

- 237
- 1
- 2
- 10
The zeros that are not shown also count. Approximately, ignoring the fact that the numbers are actually stored in binary and not in decimal, when you write both numbers with the implied zero digits included, you get:
1.0 = 1.00000000000000000*10^0000
63823 = 0000063823
As you can see, 1.0 is twice as long as 63823. Therefore it requires twice as much storage.

- 108,737
- 14
- 143
- 193
-
The anonymous downvoter presumably has got something against my didactic methods, hmm? – Joni Jun 29 '13 at 20:59
The int and double don't have decimal digits at all. The decimal representation of the int has 8 decimal digits after removing leading zeros. The int itself has room for 32 binary digits. The double has room for 53 binary digits in the mantissa and a 10-bit exponent, and a sign bit.

- 305,947
- 44
- 307
- 483