Casting

You can cause the compiler to ignore its rules of type safety and treat an object of one type as though it were an object of another type, if the types are compatible. This is called casting. Casting can be either implicit or explicit.

An implicit conversion happens automatically; the compiler takes care of it for you. If you have a short, and you assign it to a variable of type int, the compiler automatically (and silently) casts it for you. You don’t have to take any action. This is safe, because an int variable can hold any value that might have been in a short variable.

short myShort = 5;
// other code here...
int myint = myShort; // implicit conversion

Programmers often talk about this as though the short were being turned into an int. What is actually happening is that the compiler is accepting a short where it expects to find an int, because that is a safe thing to do.

Implicit conversion works only when there is no possibility of loss of data, though. Take a look at this:

int myInt;
double myDouble = 4.7;
myInt = myDouble;

If you try that assignment, the compiler gives you an error message because you can’t squeeze a decimal number into an integer space. You would lose the fractional part (.7). In fact, even if you wrote:

int myInt;
double myDouble = 4
myInt = myDouble;

the compiler would still generate an error. Although there is no fractional part to lose in this case (the double is holding a whole number), the compiler can’t take the chance that something might happen that could change the conditions, so it simply rejects all assignments of a double to an int.

This is where explicit conversions come in—when there is danger of losing data. For example, although the compiler will let you convert an int to a double implicitly (there’s no chance you can lose data), it will not let you implicitly convert a double to an int, as you’ve seen.

If you happen to know that it is perfectly safe in your particular situation to make the assignment—if you know what you are doing—you can force the compiler to accept the assignment with an explicit cast.

Again, some programmers talk about “casting the double into an integer,” but you’re not really changing either variable at all; you’re just instructing the compiler to ignore its type-safety rules for this moment and accept the assignment of the value held by the double variable to the integer variable. The compiler will comply, and it will in fact throw away any fractional part.

You specify an explicit conversion by placing the type you want to assign to in parentheses, immediately in front of the variable with the risky value:

int myDouble = 4.7;
// other code here...
int myInt = (int) myDouble; // explicit conversion

The keyword (int) is necessary to make the explicit conversion; without it the compiler will generate an error. Notice, though, that in this case, you’re slicing off the fractional value of the double; and myInt will have only the integral part (4), which is why we say you should know what you’re doing.

Sometimes you need an explicit conversion (though not often), but it is usually when you are taking an object out of a collection and you need to convert it to its “real” type, all of which we’ll discuss in a later chapter. Even then, you test to make sure the object you have is what you think it is. With this sort of explicit conversion, you are almost guaranteed to lose value sooner or later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset