twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Type Casting
|
Posted: May 7, 2004 6:17 AM
|
|
Type casting is a little different depending on whether you are dealing with primitives or doubles. With primitives, it is needed when you trying to store a value in a data type that is smaller than the data type that it is currently stored in.
For example, imagine that you were creating a program to graph math functions. The actual x and y values from the functions would need to be doubles or floats, since those values aren't always going to be whole numbers. But when you draw a graph on the screen, you will need to convert those values into pixels. You can't have half a pixel, so you may need to convert data from a double to an int.
In the case of objects, typecasting tends to involve telling the compiler what an object really is. For example, Data structures like Vectors store objects as instances of the class Object. Let's say that you store a JButton object in a Vector. While it will stay a JButton, the Vector will store it as an Object, since Object is the granddaddy of all classes.
If you access that JButton later, all the compiler will know for sure is that you are accessing an Object. Since you know that it is really a JButton, you can use a typecast to let the compiler know what it really is.
I hope this helps a little. tom
|
|