Comp 401 Metamorphosis: Casting vs. Conversion

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Pointers Prasun Dewan Comp 114.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.
Primitive Values Vs Objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal Vs == Sharing of Objects Garbage Collection.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Types in programming languages1 What are types, and why do we need them?
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
Primitive/Reference Types and Value Semantics
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Conversions of the type of the value of an expression
Unit-2 Objects and Classes
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Java Classes and Objects 3rd Lecture
Java Programming Language
Java’s Central Casting
Chapter 10 Thinking in Objects Part 2
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Chapter 6.
Chap 2. Identifiers, Keywords, and Types
Subtype Substitution Principle
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

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; 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 = new AMutablePoint (50, 100); point = (ABoundedPoint) point; Value of additional properties (upper and lower right corner)?

As Methods for Metamorphosis public class AConvertibleCartesianPoint extends AMutablePoint implements ConvertiblePoint { public AConvertibleCartesianPoint(int theX, int theY) { super (theX, theY); } public ConvertiblePoint asCartesianPoint() { return this; // could also clone public ConvertiblePoint asPolarPoint() { return new AConvertiblePolarPoint(getRadius(), getAngle()); ConvertiblePoint point = new AConvertibleCartesianPoint (50, 100); point = point.asPolarPoint(); Programmer decides what correct conversion means

Automatic Conversion of Primitive Values Java cast does not automatically convert an instance of a class to an instance of another class int intVal = 5; long longVal = intVal; double doubleVal = 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

PrimitiveAnother Primitive int intVal = 5; long longVal = intVal; double doubleVal = intVal; longVal = Long.MAX_VALUE; intVal = (int) longVal; memory 5 -1 5 5.0 9223372036854775807

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

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

Manual Wrapping and Unwrapping Integer public Integer(int value) public int intValue() Double public Double(double value) public double doubleValue() Boolean public Boolean(boolean value) public boolean booleanValue() Character public Character(char value) public char charValue() Float, Short, Long

Storage of Primitives and Wrapped Values int i = 5 int i 5 double d = 5.5 double d 5.5 Integer I = new Integer(i) Integer@8 5 Double D = new Double(d) Double@16 5.5 Integer I 8 different sizes Double D 16 same size