Join Peggy Fisher for an in-depth discussion in this video Understanding implicit and explicit conversion, part of Learning Java (2015).

- Sometimes it is necessary to change the data type of a variable. There are two ways to accomplish this change, using implicit and explicit data conversion. Although we already talked about the fact that Java is a strictly data-typed language, and it is really important to declare each variable with a specific data type, the type of data stored at that variable name can be changed both temporarily and permanently using something called implicit and explicit conversion. An implicit conversion occurs when an equation has more than one data type, such as an integer and a double.

The computer must decide which data type to use for the result of the equation. Implicit conversions always use the largest data type from the equation. For example, if you multiply 10 * 15.75, the result should be 157.50. We automatically know that we wanna include the fractional portion of our result. Another good example of this situation is probably finding the average of a group of numbers. If the numbers are integers, you still wanna make sure to get the fractional portion of the result.

If we wanted to find the average of 3, 5 and 8, we would add them together and divide by three, which would result in 5.33 repeating. When programming, the computer chooses the resulting data type based on the values in the equation. If you want to include a fractional portion, at least one value must have a decimal point. In this case, we would rewrite our equation as: (3+5+8)/3.0. Now let's talk about explicit conversion.

Explicit conversion allows the programmer to have control over the variable data by casting the variable with a new data type. For example, to find the average of three integer values, we can change the data type to a double for the sum of the numbers. Here you can see I have the word "double" in parentheses in front of the part of the equation that adds 3, 5 and 8. This will change the result to a double. Then we divide that values by 3, and the result is a double and we don't lose any precision.

Now, this is what we call a temporary conversion. The values of 3, 5 and 8 are temporarily converted to a double, so we divide by 3, we don't lose any precision. Now let's try this in NetBeans to help clarify these concepts. I already created a Data Conversion project folder. In the DataConversion.Java program, I'm gonna go ahead and show you what would happen when we do the examples from the PowerPoint. Let's add System.out.println, and let's go ahead and print out the result of adding (3 + 5 + 8)/3.

Put a semicolon at the end of our statement. Right below that, let's show the difference if we did the exact same equation, but this time let's go ahead and do (3 + 5 + 8) /3.0. Okay, and the second equation, we should get the decimal portion of our answer. In the first equation, we won't see that. Let's go ahead and run this. You can see the first equation, on line 16, gave us the result of 5.

That's because 3, 5, 8 added up to an integer, and when you divide it by an integer, you only get the integer portion of your result. The second line, because I included a double, it automatically widened the result to include the decimal portion. So I got 5.33 repeating. This concept is really important when we start to use equations such as the volume of a sphere. Let's go ahead and do that. Right below my two System.out.printlns, I'm gonna add an equation.

I'm gonna say double volume =. We know the volume of a sphere is 4/3ds, so 4/3 * pi. I'm gonna go ahead and use the math library, so I'm gonna do Math.PI. Then I'm gonna multiply that times the radius cubed. I'm gonna go ahead and put a radius of 10. I'm gonna do 10*10*10. Okay. Right now, I calculated the volume.

Let's print it out. "The volume of a sphere with radius 10 is:" and I wanna add "volume." Okay. We need a semicolon at the end. I'm gonna comment the first two lines out, since we're not using those right now. Let's go ahead and run our program. Okay. We see that "The volume of a sphere "with a radius of 10 "is 3141.59 et cetera. Okay. It gave us a decimal point because Math.PI is a double.

But the problem is, that is not the correct value. Let me show you. I'm gonna go ahead and add a second variable. Double ... I'll call it real_volume ... = 4/3.0 * Math.PI *10*10*10. Notice the only thing I changed here was the 3 to a 3.0. Now I'm gonna go ahead and print this value out, right below the first one.

I kinda ran out of room, but I can always put the plus sign and hit Enter, and I can put my next variable name down the second line, with my semicolon. Okay. This time when I run the program, it's gonna print out both the first volume and what I'm calling the real_volume. Notice if you used a calculator, you would find that 4/3ds times pi times 10 cubed is really 4188.79 et cetera. Do you see what a difference that made? It's very important that you remember that, if you divide two integers, you're only gonna get the integer portion of that result.

One more common mistake that I see often in introductory programming courses is calculating Celsius from a Fahrenheit temperature. Celsius is equal to Fahrenheit minus 32 times five divided by nine. Since five divided by nine returns an integer value of zero, the answer is always zero. Let me show you that calculation. If I create a value for fahrenheit and I set it equal to 212, and then I go ahead and print out the result of celsius, you'll see that, when I run this, the value of celsius is gonna be zero, because this equation here, (5/9), is always zero.

Even if I change fahrenheit to any value. Let's change it to 500 and try it again. It is still zero. The good news is that all of these problems can be solved easily by using implicit conversion. So if I change my (5/9) to include a decimal, (5/9.0), as it does not change the value, and I run it again, you can see I'm getting the correct Celsius value. In Java, the IDE does not allow you to use implicit conversion when assigning a double value to an integer variable.

This is called an errant condition and can cause data loss. The only way to accomplish this task is to use explicit conversion.

###### Released

2/26/2015- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs

## Share this video

## Embed this video

Video: Understanding implicit and explicit conversion