Casting Primitive Types Casting Objects

J.E.D.I

9.5 Casting, Converting and Comparing Objects

In this section, we are going to learn how to do typecasting. Typecasting or casting is the process of converting a data of a certain data type to another data type. We will also learn how to convert primitive data types to objects and vice versa. And finally, we are going to learn how to compare objects.

9.5.1 Casting Primitive Types

Casting between primitive types enables you to convert the value of one data from one type to another primitive type. This commonly occurs between numeric types. There is one primitive data type that we cannot do casting though, and that is the boolean data type. An example of typecasting is when you want to store an integer data to a variable of data type double. For example, int numInt = 10; double numDouble = numInt; implicit cast In this example, since the destination variable double holds a larger value than what we will place inside it, the data is implicitly casted to data type double. Another example is when we want to typecast an int to a char value or vice versa. A character can be used as an int because each character has a corresponding numeric code that represents its position in the character set. If the variable i has the value 65, the cast chari produces the character value A. The numeric code associated with a capital A is 65, according to the ASCII character set, and Java adopted this as part of its character support. For example, char valChar = A; int valInt = valChar; System.out.print valInt ; explicit cast: output 65 Introduction to Programming I 140 J.E.D.I When we convert a data that has a large type to a smaller type, we must use an explicit cast. Explicit casts take the following form: dataTypevalue where, dataType, is the name of the data type youre converting to value, is an expression that results in the value of the source type. For example, double valDouble = 10.12; int valInt = intvalDouble; convert valDouble to int type double x = 10.2; int y = 2; int result = intxy; typecast result of operation to int Introduction to Programming I 141 J.E.D.I

9.5.2 Casting Objects

Instances of classes also can be cast into instances of other classes, with one restriction: The source and destination classes must be related by inheritance; one class must be a subclass of the other. Well cover more about inheritance later. Analogous to converting a primitive value to a larger type, some objects might not need to be cast explicitly. In particular, because a subclass contains all the same information as its superclass, you can use an instance of a subclass anywhere a superclass is expected. For example, consider a method that takes two arguments, one of type Object and another of type Window. You can pass an instance of any class for the Object argument because all Java classes are subclasses of Object. For the Window argument, you can pass in its subclasses, such as Dialog, FileDialog, and Frame. This is true anywhere in a program, not just inside method calls. If you had a variable defined as class Window, you could assign objects of that class or any of its subclasses to that variable without casting. This is true in the reverse, and you can use a superclass when a subclass is expected. There is a catch, however: Because subclasses contain more behavior than their superclasses, theres a loss in precision involved. Those superclass objects might not have all the behavior needed to act in place of a subclass object. For example, if you have an operation that calls methods in objects of the class Integer, using an object of class Number wont include many methods specified in Integer. Errors occur if you try to call methods that the destination object doesnt have. To use superclass objects where subclass objects are expected, you must cast them explicitly. You wont lose any information in the cast, but you gain all the methods and variables that the subclass defines. To cast an object to another class, you use the same operation as for primitive types: To cast, classnameobject where, classname, is the name of the destination class object, is a reference to the source object. Introduction to Programming I 142 Figure 9.3: Sample Class Hierarchy J.E.D.I • Note: that casting creates a reference to the old object of the type classname; the old object continues to exist as it did before. The following example casts an instance of the class VicePresident to an instance of the class Employee; VicePresident is a subclass of Employee with more information, which here defines that the VicePresident has executive washroom privileges, Employee emp = new Employee; VicePresident veep = new VicePresident; emp = veep; no cast needed for upward use veep = VicePresidentemp; must cast explicitlyCasting Introduction to Programming I 143 Figure 9.4: Class Hierarchy for superclass Employee J.E.D.I

9.5.3 Converting Primitive Types to Objects and Vice Versa