Skip to main content

Bits of Java

Recommended Week

Year 1 : Weeks 14-15

Originator

Ian Holyer

Task

Find out by experiment how many bits Java uses to store these types:

You can do this by setting a variable to 1 and then repeatedly printing it and doubling it. When something wrong is printed, you will know you've just gone past the number of bits the type uses for positive numbers. Then find out if the type accepts negative numbers, and add another bit if it does.

For float and double you have to be a bit cleverer. You need to find how many bits are used for significant digits, and how many for exponents.

Which results are the most surprising?

How do the results compare with C, and what is the equivalent C type to each of the Java ones?