Professional Documents
Culture Documents
04 ConvertingAndCasting PDF
04 ConvertingAndCasting PDF
AVA
Objectives
Primitive Conversion
Primitive Casting
Object Reference Conversion
Object Reference Casting
Objectives
myVector.addElement( mycolor );
Conversion of primitives
Casting of primitives
Conversion of object references
Casting of object references
Primitives and conversion
Assignment
Method call
Arithmetic promotion
Primitive Conversion: Assignment
int i;
double d;
i=10;
d=i; // assign an int to a double variable
double i;
short s;
d=1.2345;
s=d; // assign a double value to a short
// variable
Error:
“Incompatible type for =.”
Primitive assignment conversion rules
xxxx ...
...
char
byte short
Narrowing conversions
1. byte char
2. short byte, char
3. char byte, short
4. int byte, short , char
5. long byte, short , char, int
6. float byte, short , char, int, long
7. double byte, short, char, int, long, float
Primitive Conversion: Method call
float frads;
double d;
frads=2.34567f;
d=Math.cos(frads); // pass float to method
// that expects double
double d=12.0;
Object ob=myVector.elementAt(d);
Error:
“Incompatible type for method. Explicit
cast needed to convert double to int”
Widening conversions
char
byte short
Narrowing conversions
ARE FORBIDDEN!!
Primitive Conversion: Arithmetic Promotion
1. short s=9;
2. int i=10;
3. float f=11.1f;
4. double d=12.2;
5. if (++s*i >= f/d)
6. System.out.println(“>>>>”);
7. else
8. System.out.println(“<<<<”);
Operand Conversion
• byte int
• short int
• char int
otherwise NO CONVERSION
Binary operators
Rule conversion
int i=5;
double d=i;
Error:
“Explicit cast needed to convert short to byte”
short s=259;
byte b=(byte)s; // Explicit cast
System.out.println(”b= “+b);
0000000100000011
b=(byte)s
00000011
Casting a value to a wider value is always permitted
but never required; if you omit the cast, an
implicit conversion will be performed on your
behalf
1. int i=2;
2. double radians;
. // hundreds of
. // lines of
. // code
600. radians=(double)i;
source target
1. Assignment conversion
2. Method-call conversion
3. Casting
Oldtype=new Oldtype();
Newtype y=x; // reference assignment
// conversion
Converting Oldtype to Newtype
Oldtype is a Oldtype is an Oldtype is an
class interface array
Fruit
Citrus
(implements
Squeezable)
Compiler Error!!
Grapefruit g=new Grapefruit();
Squeezable squee=g; // No problem
Grapefruit g2=squee; // Error
Fruit fruits[];
Lemon lemons[];
Citrus citruses[]=new Citrus[10];
for (int i=0; i<10; i++) {
citruses[i]=new Citrus();
}
Fruits=citruses; // No problem
lemons=citruses; // Error
Object Method-Call Conversion:
1. classes
2. interfaces, or
3. arrays
While an object’s class is unchanging, it may be
referenced by variables of many different
types
1001 ... 0110 1001 ... 0110 1001 ... 0110 1001 ... 0110
1. non-final class
2. final class
3. interface
4. array
Compile-time rules
Oldtype is a Oldtype is a Oldtype is an Oldtype is an array
non-final final class interface
class
Fruit
Citrus
(implements
Squeezable)