Published using Google Docs
Java Basics 103 - Short and Float Variables
Updated automatically every 5 minutes

Java Basics 103 – Short and Float Variables

STEPS

1) Browse http://ideone.com/O5GIga 

http://ideone.com/O5GIga 

2) Change the variables properties to Float Data Types.

Change line no.13 to:

float first_number, second_number, answer;

Change line no.14, 15 to:

first_number = 10.5f;
second_number = 20.8f;

Outcome:

http://ideone.com/NV31e4 

3) Change the Operator to Division

Change the operator in line no.16 from “+” to “/”.

Outcome:

http://ideone.com/uvsGVV 

4) Double Variables

Change the Data Type for variables in line no.13 to Double.

Outcome:

http://ideone.com/S4VYLO 

Lessons:

When you use Float Data Type, the answer was 0.5048077. Java has taken the first 6 numbers after the point and then rounded up the rest.

However, the double variable type can hold more numbers than float. (Double is a 64 bit number and float is only 32 bit.)

REFERENCE

http://www.homeandlearn.co.uk/java/short_float_variables.html