Question: Try to store a floating-point number in a float. Type the following: float myFloat = 5.5; Compile. Does that compile? It does not compile because

Try to store a floating-point number in a float. Type the following:

float myFloat = 5.5;

Compile. Does that compile?

It does not compile because Java recognizes every floating point literal as a double. However, if we wanted to store 5.5 in a float, we could. We just have to cast it as a float. Modify the previous line as shown below:

float myFloat = 5.5F;

Compile the program. That F at the end of 5.5 denotes a float value.

Type the following line. Do you think it will compile?

float myFloat2 = 1;

Any compilation errors? Should be none this time because Java recognizes all integer literals (such as 1) as ints. Going from an int to a float is an example of a widening conversion. A widening primitive conversion does not lose information about the overall magnitude of a numeric value. You can go from the data type byte to short, int, long, float or double. You can go from int to long, float or double and you can go from float to double.

Lets try some of them.

Declare a double variable named doubleVal and set it equal to myFloat.

double doubleVal = myFloat;

Declare a byte variable named byteVal and set it equal to 4.

Declare an int named intVal and set it equal to byteVal.

Declare a short called shortVal and set it equal to 247.

Declare a long named longVal and set it equal to shortVal.

Use three System.out.println statements to print the variables doubleVal, intVal and longVal.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!