Precision Level

Precision Level What is the difference between a float and a double type in Java? I'm currently covering casting in my programming class (Java), but I'm getting confused with when casting is nece...


Precision Level

Precision Level
What is the difference between a float and a double type in Java?

I'm currently covering casting in my programming class (Java), but I'm getting confused with when casting is necessary when using float or double numbers. I know that float values are 32 bits and double values are 64 (and thus give different levels of precision). What I'm confused is in a case such as this:

float f = (float) 3.14;

Why is casting necessary? Isn't 3.14 already within a float's range; why would it have to be casted again as a float?
Thanks for the help :]

It comes down to the fact that Java has tight type safety.

By default java assumes a manually specified number to be a double, as a double would more precisely cover more numbers that you could type in. You have to cast between doubles and float explicitly. Java is helping you to not do something stupid (loose precision) by making you be absolutely sure about what your doing.

If you want to manually specify floats I always do the following to avoid annoying casting

float f = 3.14f;

 
Trimble Spectra Precision LL500 Level w 2ea LD 100N
Trimble Spectra Precision LL500 Level w 2ea LD 100N
Paypal   US $100.00
LS Starret 12 Double Plumb Precision Bench Level 132
LS Starret 12 Double Plumb Precision Bench Level 132
Paypal   US $75.00
MASTER PRECISION MACHINIST MACHINE LEVEL 0002 10
MASTER PRECISION MACHINIST MACHINE LEVEL 0002 10
Paypal   US $97.00
12 MASTER PRECISION LEVEL FOR MACHINIST TOOL NEW 0005
12 MASTER PRECISION LEVEL FOR MACHINIST TOOL NEW 0005
Paypal   US $75.90
Spectra Precision LL500 Rotary Laser Level No Reserve
Spectra Precision LL500 Rotary Laser Level No Reserve
Paypal   US $390.00
VINTAGE LS STARRETT NO 98 12 12 PRECISION LEVEL
VINTAGE LS STARRETT NO 98 12 12 PRECISION LEVEL
Paypal   US $33.99
TRIMBLE SPECTRA PRECISION 1242 AUTOMATIC LASER LEVEL
TRIMBLE SPECTRA PRECISION 1242 AUTOMATIC LASER LEVEL
Paypal   US $365.00
L100M MX Laser Precision Level with Accessories
L100M MX Laser Precision Level with Accessories
Paypal   US $75.00
Starrett 98 precision level with case millwright
Starrett 98 precision level with case millwright
Paypal   US $50.00
EDA precision level
EDA precision level
Paypal   US $155.00
STARRETT precision 12 level number 98
STARRETT precision 12 level number 98
Paypal   US $49.99
Spectra Precision 175 Laser Level Tripod Case
Spectra Precision 175 Laser Level Tripod Case
Paypal   US $300.00
STARRETT 15 MASTER PRECISION LEVEL 199 CASE
STARRETT 15 MASTER PRECISION LEVEL 199 CASE
Paypal   US $237.50
Bruel Kjaer Impulse Precision Sound Level Meter 2209
Bruel Kjaer Impulse Precision Sound Level Meter 2209
Paypal   US $199.00
8 MASTER PRECISION LEVEL FOR MACHINIST TOOL NEW 0005
8 MASTER PRECISION LEVEL FOR MACHINIST TOOL NEW 0005
Paypal   US $62.90
Spectra Precision LL300 Laser Level Receiver HR350
Spectra Precision LL300 Laser Level Receiver HR350
Paypal   US $310.00
Spectra Precision LP40 4 Beam Point Laser Level PLS
Spectra Precision LP40 4 Beam Point Laser Level PLS
Paypal   US $209.00
BK Precision 4231 Sound Level Calibrator Users Manual
BK Precision 4231 Sound Level Calibrator Users Manual
Paypal   US $30.00
SPECTRA Precision Trimble LP20 Laser Plumb level NEW
SPECTRA Precision Trimble LP20 Laser Plumb level NEW
Paypal   US $160.00
SPECTRA PRECISION GL722 DUAL GRADE ROTATING LASER LEVEL
SPECTRA PRECISION GL722 DUAL GRADE ROTATING LASER LEVEL
Paypal   US $2,349.15
6 X 1 1 2 X 6 PRECISION FRAME SPIRIT LEVELS LEVEL
6 X 1 1 2 X 6 PRECISION FRAME SPIRIT LEVELS LEVEL
Paypal   US $69.95
WILD N3 Precision Survey Level w Carrying Case
WILD N3 Precision Survey Level w Carrying Case
Paypal   US $1,995.00
Starrett Master Precision Level Machinists Good Shape
Starrett Master Precision Level Machinists Good Shape
Paypal   US $419.99
RION NA 80 precision measuring level meter
RION NA 80 precision measuring level meter
Paypal   US $199.00
Precision Frame Spirit Level 12 x 12 Inch NEW
Precision Frame Spirit Level 12 x 12 Inch NEW
Paypal   US $116.95
VOGEL WS35 S10 C81 230 PRECISION LEVEL SWITCH W LED NNB
VOGEL WS35 S10 C81 230 PRECISION LEVEL SWITCH W LED NNB
Paypal   US $325.00
VOGEL WS32 2 230 V57 PRECISION LEVEL SWITCH 1 SP NNB
VOGEL WS32 2 230 V57 PRECISION LEVEL SWITCH 1 SP NNB
Paypal   US $195.00
Master Precision Level 8 Graduation 00005 NEW
Master Precision Level 8 Graduation 00005 NEW
Paypal   US $74.98
K E Precision Tilting Level Perfect Optics 9092 3A
K E Precision Tilting Level Perfect Optics 9092 3A
Paypal   US $1,199.00
Trimble Spectra Precision Optical AL220 Auto Level NEW
Trimble Spectra Precision Optical AL220 Auto Level NEW
Paypal   US $189.00
Trimble Spectra Precision Optical AL224 Auto Level NEW
Trimble Spectra Precision Optical AL224 Auto Level NEW
Paypal   US $229.00
G R 1981 Precision Sound Level Monitor Inst Manual
G R 1981 Precision Sound Level Monitor Inst Manual
Paypal   US $30.00
Brand New Starrett 12 PRECISION MACHINIST LEVEL
Brand New Starrett 12 PRECISION MACHINIST LEVEL
Paypal   US $169.00
B K TYPE 2305 LEVEL RECORDER PRECISION BRUEL KJAER
B K TYPE 2305 LEVEL RECORDER PRECISION BRUEL KJAER
   US $195.00
spectra precision trimble hv 601 laser level rotary
spectra precision trimble hv 601 laser level rotary
Paypal   US $1,399.99
LL200 Spectra Precision Laser Level with Detector NEW
LL200 Spectra Precision Laser Level with Detector NEW
Paypal   US $599.00
LEICA PRECISION DIGITAL LEVEL 723289 DNA03 03mm NIB
LEICA PRECISION DIGITAL LEVEL 723289 DNA03 03mm NIB
Paypal   US $5,245.00

Leave a Reply