1 / 10

Comp 401 Metamorphosis: Casting vs. Conversion

Comp 401 Metamorphosis: Casting vs. Conversion. Instructor: Prasun Dewan. Prerequisite. Interfaces Types Math Deep vs. Shallow Copy (Optional). Metamorphosis?. Point point = new ACartesianPoint (50, 100); point = ( APolarPoint ) point;.

ida
Download Presentation

Comp 401 Metamorphosis: Casting vs. Conversion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp 401Metamorphosis: Casting vs. Conversion Instructor: Prasun Dewan

  2. Prerequisite • Interfaces • Types Math • Deep vs. Shallow Copy (Optional)

  3. Metamorphosis? Point point = newACartesianPoint (50, 100); point = (APolarPoint) point; Class cast exception, Java cast does not automatically convert an instance of a class to an instance of another class It does not even automatically (deep) copy Ambiguous semantics Point point = newAMutablePoint (50, 100); point = (ABoundedPoint) point; Value of additional properties (upper and lower right corner)?

  4. As Methods for Metamorphosis publicclassAConvertibleCartesianPoint extendsAMutablePointimplementsConvertiblePoint { publicAConvertibleCartesianPoint(inttheX, inttheY) { super (theX, theY); } publicConvertiblePointasCartesianPoint() { returnthis; // could also clone } publicConvertiblePointasPolarPoint() { return newAConvertiblePolarPoint(getRadius(), getAngle()); } } ConvertiblePoint point = newAConvertibleCartesianPoint (50, 100); point = point.asPolarPoint(); Programmer decides what correct conversion means

  5. Automatic Conversion of Primitive Values Java cast does not automatically convert an instance of a class to an instance of another class intintVal = 5; longlongVal = intVal; doubledoubleVal = intVal; longVal = Long.MAX_VALUE; intVal = (int) longVal; In each of the multi-type assignments, a primitive memory value copied and stored in another primitive memory value of different size In the last case, cast forces conversion

  6. PrimitiveAnother Primitive intintVal = 5; longlongVal = intVal; doubledoubleVal = intVal; longVal = Long.MAX_VALUE; intVal = (int) longVal; memory 5 -1 5 5.0 9223372036854775807

  7. Primitive  Object intVal = (int) longVal; Integer integerVal = intVal; int intVal2 = integerVal;

  8. Wrapper Types “Joe Doe”.toString() Object 5.toString() “Joe Doe”.equals(5) Extends “Joe Doe”.equals(new Integer(5)) AString History String Number 5 + new Integer(5) 5 + (new Integer(5)).intValue() Integer Double Character Boolean Wrapper classes Wraps, contains Primitive types int double char bool Java automatically converts between primitive and wrapper types when assigning an expression to a variable

  9. Manual Wrapping and Unwrapping • Integer • public Integer(int value) • publicintintValue() • Double • public Double(double value) • publicdoubledoubleValue() • Boolean • public Boolean(boolean value) • publicbooleanbooleanValue() • Character • public Character(char value) • publiccharcharValue() • Float, Short, Long

  10. Storage of Primitives and Wrapped Values inti = 5 inti 5 double d = 5.5 double d 5.5 Integer I = newInteger(i) Integer@8 5 Double D = newDouble(d) Double@16 5.5 Integer I 8 different sizes 16 Double D same size

More Related