objects first with java a practical introduction using bluej casting week 22 2.0

11
Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

Upload: tobias-phelps

Post on 17-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

3 Casting numbers We can easily assign an int to a double because there is no possibility of a loss of precision In the following example, the value 3.0 will be assigned to d int i = 3; double d = i;

TRANSCRIPT

Page 1: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

Objects First With JavaA Practical Introduction Using BlueJ

Casting

Week 22

2.0

Page 2: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

2

Main concepts to be covered

• Casting numbers• Casting objects• The instanceof operator

Page 3: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

3

Casting numbers• We can easily assign an int to a

double because there is no possibility of a loss of precision

• In the following example, the value 3.0 will be assigned to d

int i = 3;double d = i;

Page 4: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

4

Casting numbers• There would be a compilation error

though if we tried to write:double d = 3.9;int i = d;

• If we want to assign a double to an int (or, for example, to a float) then we need to tell the compiler that we accept the possible loss of precision

Page 5: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

5

Casting numbers• This process is known as casting• We need to cast the number to an

int by writing (int) in front of it• In the following example, the value

3 will be assigned to idouble d = 3.9;int i = (int) d;

Page 6: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

6

Casting objects• Consider the dome-v2 project• The CD class has the following

method to return the number of tracks

public int getNumberOfTracks() { return numberOfTracks; }

Page 7: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

7

Casting objects• Suppose we wanted to write a method

in the Database class to print the number of tracks of a CD that was stored at a particular position in the array list

• The Item object returned from the get method would need to be cast into a CD object in order to call the getNumberOfTracks method

Page 8: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

8

Casting objectspublic void printNumberOfTracks(int index){ CD cd = (CD) items.get(index); System.out.println("The number of tracks on the CD is " +

cd.getNumberOfTracks());}

Page 9: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

9

The instanceof operator• The printNumberOfTracks method could be

improved by checking whether the returned Item object is actually a CD object, and only casting it to CD if it is. This could be achieved using the instanceof operator.

• The expressionanObject instanceof AClass evaluates to true if the type of anObject is AClass (or some subclass of Aclass). Otherwise it evaluates to false.

Page 10: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

10

The instanceof operatorpublic void printNumberOfTracks(int index){ Item item = items.get(index); if (item instanceof CD) { CD cd = (CD) item; System.out.println("The number of tracks on the CD is " +

cd.getNumberOfTracks() + "."); } else { System.out.println("The item is not a CD."); }}

Page 11: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0

11

Review• Casting can be used with numbers to,

for example, assign a double to an int.

• Casting can be used with objects when it is required to call a method that only exists in a subclass of the declared type.

• The instanceof operator can be used to check the type of an object.