Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0.
-
Upload
tobias-phelps -
Category
Documents
-
view
212 -
download
0
description
Transcript of Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0.
![Page 1: Objects First With Java A Practical Introduction Using BlueJ Casting Week 22 2.0.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083119/5a4d1b0f7f8b9ab05998dba1/html5/thumbnails/11.jpg)
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.