Java Reference
In-Depth Information
Bam! You are now inside your brand-new, creeper-proof house, which was
built to the exact dimensions you specified with your
width
and
height
variables.
A right-click will open the door, by the way.
Different Kinds of Numbers
Java makes a distinction between
integer
whole numbers (with no decimal
point, like 8) and
floating-point
numbers with a fractional part (with a decimal
point), like 10.125.
For plain old whole numbers, you use an
int
, like we've seen.
Floating-point numbers can be
float
or
double
. A
double
is larger and can store
numbers much more precisely, but at the cost of needing more space and
power to manage. But computers are fast and have plenty of storage these
days, so almost everyone just uses
double
any time they need a double-preci-
sion, floating-point, fractional number.
When you type a number with a decimal point in Java, it assumes you've
typed a
double
:
3.1415
// assumed to be a double
But if you really need the number to be a
float
, you have to stick the letter
f
on the number:
3.1415f
// now it's a float, not a double.