Implicit and Explicit Casting

In this section you explore the concepts of the unifying types and casting.

1. Open the ArithmeticDemo.java file that uses integer values to calculate a sum, difference, and average. Change the class name to ArithmeticDemo3, and immediately save the file as ArithmeticDemo3.java.

2. In the previous version of the program, the average was calculated without decimal places because when two integers are divided, the result is an integer. To compute a more accurate average, change the data type for the average variable from int to double.

3. Save, compile, and execute the program. As the sample execution in Figure 2-42 shows, the program compiles and executes, but the average is still not accurate. The average of 20 and 19 is calculated to be 19 because the decimal portion of the arithmetic result is lost. The program executes because the result of an integer divided by an integer is an integer, and when the integer is assigned to the double, automatic type conversion takes place.

Figure 2-42 Typical execution of ArithmeticDemo3 application see image.

4. Change the statement that computes the average to include a cast as follows:

average = (double) sum / 2;

5. Save, compile, and execute the program. As shown in Figure 2-43, now the program displays a more accurate average. The integer sum has been cast to a double, and when the double is divided by the integer, the result is a double, which is then assigned to average.

Figure 2-43 Typical execution of ArithmeticDemo3 application after addition of a cast operation for the average. see image.

6. Change the statement that computes the average to include a second set of parentheses, as follows:

average = (double)(sum / 2);

7. Save, compile, and execute the program. Now, the fractional portion of the result is omitted again. That's because the result of sum / 2 is calculated first, and the result is an integer. Then the whole-number result is cast to a double and assigned to a double-but the fractional part of the answr was already lost and casting is too late. Remove the newly added parentheses, save the program, compile it, and execute it again to confirm that the fractional part of the answer is reinstated.

8. As an alternative to the explicit cast in the division statement in the ArithmeticDemo program, you could write the average calculation as follows:

average = sum / 2.0;

In this calculation, when the integer sum is divided by the double constant 2.0, the result is a double. The result then does not require any cast to be assigned to the double average without loss of data. Try this in your program.

9. Go to the Java Web site(www.oracle.com/technetwork/java/index.html), select Java APIs, and then select Java SE 7. Scroll through the list of All Classes, and select PrintStream, which is the data type for the out object used with the println() method. Scroll down to view the list of methods in the Method Summary. As you did in a previous exercise, notice the many versions of the print() and println() methods, includes one that accepts String, an int, and a long. Notice, however, that no verions accept a byte or a short. That's because when a byte or short is sent to the print() or println() method, it is automatically promoted to an int, so that version of the method used.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.