We're going to finish with a brief discussion of converting from one type to another. We've already touched on that several times, but it's worthwhile to take a look at it explicitly. Now, the thing about the computer language and the concept of data type. Is the variables involved in a data type operation have to be of the right type. It's only defined for that, that's why we appreciate the idea that the Java Compiler can check for type errors in the code. If we try to add an integer to a string, it'll tell us, well, if we try to add a bullion to a double. It'll tell us that you can't do that type of operation, so there's a string times two, and it's going to say. You can't multiply an integer times a string, that's an error, and that's a good thing that the Java Compiler will tell us about that error. Because sometimes there might be conversions that happen automatically that we might not expect. But a lot of times, as we've already seen, we want to convert from one type to another. To make types match in an operation where it makes sense. So, with the built-in types it's really converting as an essential aspect of programming. We have to worry about it, there's three ways that it can happen, so we've already talked about automatic. If we have plus and one of the arguments is a string, it's automatically going to convert, and the others a number. It's automatically going to convert that number to a string to get concatenation done. If you're trying to use a float and an integer in the same expression. It'll go ahead and make the conversion if there's not going to be any loss of precision. So 11 times 0.25, it'll convert the 11 to a float, and that'll be the right type, and it can perform the operation. Sometimes we need to explicitly define a type conversion like, where the function call. Integer.parseInt returns an int and it's going to explicitly make the conversion of the string into an int value. Math.round is another example. And then there's a third type of type conversion, it is called a cast. And so that's what we use for values that belong to multiple types like small integers, like, 2 or 12, can be either short, int, or long. And also double values, we can truncate them to int values, if sometimes that's what we want to do in the computation. And the way that we make these types of type conversion is just put the type that we want in parentheses before the number. So if we say int of 2.71828, it will truncate to give an int value that can be used in another expression. So those are examples, 11 times int to 2.25, that is going to be truncated to a 0, so the result will be 0. And we look at these types of expressions, and there's many examples in the book and in the exercises. And it's important to always know the type of your data, because if you're not aware of the type of data, it can lead to problems. Sometimes type conversion gives sort of counter-intuitive results, but it's much easier to understand with some practice. And that's why we highlight it right at the beginning, so here's just a couple of questions that, again, you might find on a quiz. So 7 slash 2 times 2.0, so what's the type of that, and what's the value? Well, 7 over 2 is inside parentheses, so that's going to be integer division, so it's going to throw away the remainder, it's just going to be 3. And then, multiplying by 2.0 that 3 will get converted to a double, so then the operation star is two doubles and the answer will be 6.0. If you do 7/2.0, then you get, inside the parentheses, 3 and a half, because the 7 will get converted to a double. Because the operation is a double says 3.5, and then, that 2 on the right, will get converted to a double. So we have a multiply with two doubles and you get 7.0, it also gets a little confusing with strings. So, the string 2 plus 2, well, Java automatically converts the right hand argument, which is a 2, to a string. And so you get 22 and the result's a string, 2.0 plus 2, again, that's a string, if either argument's a string. It's going to convert the other one to a string, and so anyway, those are some examples of type conversion. And it's a good idea to work through some examples like this, so this is just a instructive modern day story about type conversion. Again, why do we have different numeric types? It's because if we want to have a big range, we're going to have to use memory for integers. And if we want more precision, we're going to have to use more bits for floating point, and that's why we have these different types. Now, but the thing is, that because of the restriction on the range, it might be impossible to do a conversion sometimes. So, for example, say I have the number 70,000, and I say, okay, I want to cast that to a short, so I could write short 70,000. Well, that's not possible because short values have to be between minus 32768 and 32767, that's only 16 bits, can't do that conversion. Well, what do you do if there's an impossible conversion, or what does the system do if there's an impossible conversion? Well, there's a couple of approaches you could use. So the one that we advocate the most is, make sure that you avoid doing it in the first place. If you write that cast, make sure that you know that the thing is in the right range. Java's approach is say okay, there's going to be a well-defined result and you're going to have to live with it. And you can check and so forth, but anyway, that's Java's approach, and another approach is to not check for it, not live with it, just crash. And by crash, we mean actually crash, a type conversion error of this type actually led to the Ariane 5 rocket blowing up in 1996. They switched to a different representation, and there was a cast that was impossible. And one of the controlled variables had a bad value that caused the rocket to crash, you have to pay attention to the type of your data. Here's an example of type conversion put to good use, and this is our last program of the day. So we've got Math.random that gives us a pseudo-random double value. What if we want to get a pseudo-random integer value between 0 and then -1 for any given math? If we want to roll a die, we'd make N equals 6, so if we want to draw a card, we'd make N equals 52. So, what we're going to do is, take the value N from the command line, again, string to in system method. Then we're going to get our random double value, a number between 0 and 1, but not less than 1. And then what we do is multiply r x N, that's going to give a result that's between 0 and N, but less than N. And then we're going to say, okay, now you can truncate that, and that'll give you, that first the N gets converted to double. To do the r times N, and then there's the cast to truncate, once that's done and the result is an integer value between zero and N minus 1. That we just print out, so now we can roll a die, and we get different random values, or if we want a random value of less than 10,000. We can get that, too, so that's knowing the types of your variables and using them for a useful computation. And we'll use this one actually a lot in this course, so that's the summary. A data type is the set of values and the set of operations on those values. There is a lot of commonly used built in data types for Java that we've seen programs that use them. There's string for input and output, which is strings of characters. There's integers and doubles that we use for science and math calculations and programs. And there's bullion, that's computing with true/false values, that we're going to use for decision-making in programs. So in Java you must declare the types of your variables, that's not true in all programming languages. You must convert from one type to another when necessary and you must identify and resolve type errors in order to compile your code. Java Compiler will not let you go on if you're doing something like multiplying a string by an integer. You have to pay attention to the type of your data. And I emphasize this, because it's an important distinction, between Java, and some other popular programming languages. The compiler's your friend, it's going to help you Identify and fix the type errors in your code. And you're going to have much better and more reliable code as a result. Next time, we'll talk about writing programs that have more complicated control structures. And perform lots more interesting tasks.