Casting


The need for casting of references occurs when you, the programmer, know that an object is of a certain type but the compiler couldn't possibly know what the type is. Casting allows you to tell the compiler that it can safely assign an object of one type to a reference of a different type.

Sun's introduction of parameterized types removed much of the need for casting in Java. Prior to J2SE 5.0, since the collection classes in Java did not support parameterized types, everything that went into a collection had to go in as an Object reference, as shown by the signature for the add method in the List interface:

 public boolean add(Object o) 

Since everything went in as an Object, you could only retrieve it as an -Object:

 public Object get(int index) 

You, the developer, knew you were stuffing Student objects into a collection, but the collection could only store Object references to the Students. When you went to retrieve the objects from the collection, you knew that the collection stored Student objects. This meant that you typically cast the Object references back to Student references as you retrieved them from the collection.

To cast, precede a target expression with the reference type that you want to cast it to, in parentheses:

 Student student = (Student)students.get(0); 

In this example, the target is the result of the entire expression students.get(0). You could have parenthesized the target to be more explicit:

 (Student)(students.get(0)); 

Note the absence of any spaces between the cast and the target. While you may interject spaces between a cast and its target, the more commonly accepted style specifies no spaces.

It is important to understand that casting does nothing to change the target object. The cast to Student type only creates a new reference to the same memory location. Since this new reference is of type Student, the Java compiler will then allow you to send Student-specific messages to the object via the reference. Without casting, you would only be able to send messages defined in Object to the Student object.

Using parameterized types, there are few reasons to cast object references. If you find yourself casting, determine if there is a way to avoid casting by using parameterized types.

As an exercise, the following test demonstrates the "old way" of iterating through a collectionwithout a for-each loop and without parameterized types. Note that this code will still work under J2SE 5.0, although you may receive warnings because you are not using the parameterized collection types.

 public void testCasting() {    List students = new ArrayList();    students.add(new Student("a"));    students.add(new Student("b"));    List names = new ArrayList();    Iterator it = students.iterator();    while (it.hasNext()) {       Student student = (Student)it.next();       names.add(student.getLastName());    }    assertEquals("a", names.get(0));    assertEquals("b", names.get(1)); } 

Casting Primitive Types

You cannot cast primitive types to reference types or vice versa.

There is another form of casting that you will still need to use in order to convert numeric types. I will discuss numeric conversion in Lesson 10 on mathematics.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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