Casting


As we have seen, there are many variable types in the Java language. There may be times when you need to temporarily convert a variable of one type into another type. This operation is called casting. A common use of casting is to convert the return type of a method into a more specific type. For example, methods that are designed to work with all kinds of objects might have a return type of Object . You may need to cast the Object into a String for instance.

The cast operator is a pair of parentheses surrounding the type to which the variable will be cast. The cast operator precedes the variable to be cast.

 (variable_type)variable_name; 

Casting can be performed on both primitive and reference types. With primitive types, widening conversions (e.g., float to double ) are performed automatically. No cast is needed. Narrowing conversions (e.g., double to float ) that might involve a loss of precision require an explicit cast. When it comes to integers and floating point numbers , an integer type will automatically be cast into a floating point type, but a floating point type must be explicitly cast into an integer type. A reference type object can only be cast to a subclass object. Primitive types cannot be cast to reference types.

Example: Casting

Many of the methods in the Java API are designed to work with a wide variety of types. A method may have a return type of Object , meaning it can return any reference type. When you use such a method, you can cast the returned Object into the reference type you want.

The CastDemo class defines a Vector that stores three BlackBody3 objects. The BlackBody3 class was presented in the "Using Final Variables" example earlier in this chapter. The getElement() method is written to return the BlackBody3 object at a specified index. The method does this by calling the Vector data member's get() method. This method returns an Object . Since we know the Vector stores BlackBody3 objects, we cast the return value to a BlackBody3 object and return the result of the cast.

The get() method from the Vector class can throw an exception. As such, the get() call is placed in a try block. Exception handling is described in more detail in Chapter 12. The second half of the main() method demonstrates how to cast integral to floating-point types and vice versa.

 import java.util.*; public class CastDemo {   private Vector data;   //  Create a Vector object and load it with   //  some BlackBody3 objects.   public CastDemo() {     data = new Vector();     data.add(new BlackBody3(0.85, 2000.0));     data.add(new BlackBody3(0.9, 4536.0));     data.add(new BlackBody3(0.7, 1789.0));   }   //  The get() method from the Vector returns   //  an Object.  It is cast into a BlackBody3   //  object   public BlackBody3 getElement(int index) {     BlackBody3 body;     try {       body = (BlackBody3)data.get(index);     }     catch (ArrayIndexOutOfBoundsException e) {       body = (BlackBody3)data.get(0);     }     return body;   }   public static void main(String args[]) {     CastDemo demo = new CastDemo();     BlackBody3 first = demo.getElement(0);     System.out.println("heating is "+                           first.getHeating());     //  This demonstrates integer-floating point casts     float f = 12.34F;   //      int i = f;   This won't work     int i = (int)f;     System.out.println("i is " + i);     f = i;     System.out.println("f is " + f);   } } 

Output ”

 heating is 77.10792 i is 12 f is 12.0 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net