SlideShare a Scribd company logo
Converting and
Casting

Ben Abdallah Helmi Architect
1
J2EE
Explicit and Implicit Type Changes
• Button btn = (Button) (myVector.elementAt(5));
• Of course, the sixth element of the vector must be
capable of being treated as a Button Compiletime rules
and runtime rules must be observed

Ben Abdallah Helmi Architect
2
J2EE
Primitives and Conversion
•

Both primitive values and object references can be converted and cast, so you must consider

four general cases:
>

>

Casting of primitives

>

Conversion of object references

>

•

Conversion of primitives

Casting of object references

The simplest topic is implicit conversion of primitives. All conversion of primitive data types

takes place at compile time; this is the case because all the information needed to determine whether the
conversion is legal is available at compile time.
•

Conversion of a primitive might occur in three contexts or situations:
>

Assignment

>

Method call

>

Arithmetic promotion

Ben Abdallah Helmi Architect
3
J2EE
Primitive Conversion: Assignmen
• A boolean cannot be converted to any other type.

• A nonboolean can be converted to another nonboolean type,
provided the conversion is a widening conversion
• A nonboolean cannot be converted to another nonboolean
type if the conversion would be a narrowing conversion

Ben Abdallah Helmi Architect
4
J2EE
From a byte to a char
From a short to a byte or a char
From a char to a byte or a short
From an int to a byte, a short, or a char
From a long to a byte, a short, a char, or an int
From a float to a byte, a short, a char, an int, or a
long
• From a double to a byte, a short, a char, an int, a
long, or a float
•
•
•
•
•
•

Ben Abdallah Helmi Architect
5
J2EE
Assignment Conversion, Narrower
Primitives, and Literal Values
Java’s assignment conversion rule is sometimes inconvenient when a literal value is assigned to

a primitive. By default, a numeric literal is either a double or an int, so the following line of
code generates a compiler error:
float f = 1.234;
The literal value 1.234 is a double, so it cannot be assigned to a float variable. You might assume
that assigning a literal int to some narrower integral type (byte, short,or char) would fail to
compile in a similar way. For example, it would be reasonable to assume that all of the following
lines generate compiler errors:
byte b = 1;
short s = 2;
char c = 3;
In fact, all three of these lines compile without error. The reason is that Java relaxes its assignment
conversion rule when a literal int value is assigned to a narrower primitive type (byte,short, or
char), provided the literal value falls within the legal range of the primitive type.
This relaxation of the rule applies only when the assigned value is an integral literal. Thus the
second line of the following code will not compile:
int i = 12;
byte b = i

Ben Abdallah Helmi Architect
6
J2EE
Primitive Conversion: Method Call
1. float frads;
2. double d;
3. frads = 2.34567f;
4. d = Math.cos(frads); // Pass float to method
// that expects double
1. double d = 12.0;
2. Object ob = myVector.elementAt(d);
Fortunately, the rule that governs which method-call
conversions are permitted is the same rule that governs
assignment conversions. Widening conversions (as shown in
Figure 4.3) are permitted; narrowing conversions are
forbidden

Ben Abdallah Helmi Architect
7
J2EE
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("<");
Ben Abdallah Helmi Architect
8
J2EE
• For unary operators, two rules apply, depending on the
type of the single operand: If the operand is a byte, a
short, or a char, it is converted to an int (unless the
operator is ++ or --, in which case no conversion
happens). Else there is no conversion.
• For binary operators, there are four rules, depending on
the types of the two operands: If one of the operands is
a double, the other operand is converted to a double.
Else if one of the operands is a float, the other operand
is converted to a float.Else if one of the operands is a
long, the other operand is converted to a long. Else
both operands are converted to ints.
shows Java’s unary and binary arithmetic operators.
Ben Abdallah Helmi Architect
9
J2EE
Primitives and Casting
Casting is explicitly telling Java to make a conversion. A casting operation may widen or narrow its argument. To cast, just precede a
value with the parenthesized name of the desired type. For example, the following lines of code cast an int to a double:

1. int i = 5;
2. double d = (double)i;
Of course, the cast is not always necessary. The following code, in which the cast has been
omitted, would do an assignment conversion on i, with the same result as the previous example:
1. int i = 5;
2. double d = i;
Casts are required when you want to perform a narrowing conversion. Such conversion will
never be performed implicitly; you have to program an explicit cast to convince the compiler
that what you really want is a narrowing conversion. Narrowing runs the risk of losing information the cast tells the compiler that you
accept the risk.
1. short s = 259;
2. byte b = (byte)s; // Explicit cast
3. System.out.println("b = " + b);
b=3

Ben Abdallah Helmi Architect
10
J2EE
1. int i = 2;
2. double radians;
. // Hundreds of
. // lines of
. // code
600. radians = (double)i;
The cast in the last line is not required, but it serves as a good reminder
to any readers (including yourself) who might have forgotten the type
of radians.
Two simple rules govern casting of primitive types:
You can cast any non-boolean type to any other non-boolean type.
You cannot cast a boolean to any other type; you cannot cast any other
type to a boolean. Note that although casting is ordinarily used when
narrowing, it is perfectly legal to cast when widening. The cast is
unnecessary, but it provides a bit of clarity.
Ben Abdallah Helmi Architect
11
J2EE
Object Reference Conversion
Object reference conversion is more complicated than
primitive conversion, because there are more possible
combinations of old and new types—and more
combinations mean more rules.
Reference conversion, like primitive conversion, takes place
at compile time, because the compiler has all the
information it needs to determine whether the conversion
is legal. Later you will see that this is not the case for
object casting.
The following sections examine object reference
assignment, method-call, and casting conversions.
Ben Abdallah Helmi Architect
12
J2EE
Object Reference Assignment
Conversion
Object reference assignment conversion happens when you
assign an object reference value to
a variable of a different type. There are three general kinds of
object reference type:
• A class type, such as Button or FileWriter
• An interface type, such as Cloneable or LayoutManager
• An array type, such as int[][] or TextArea[]
Generally speaking, assignment conversion of a reference looks
like this:
1. Oldtype x = new Oldtype();
2. Newtype y = x; // reference assignment conversion
Ben Abdallah Helmi Architect
13
J2EE
Recall that with primitives, conversions were permitted, provided they were widening
conversions. The notion of widening does not really apply to references, but a similar principle
is at work. In general, object reference conversion is permitted when the direction of
the conversion is “up” the inheritance hierarchy; that is, the old type should inherit from the
new type. This rule of thumb does not cover all nine cases, but it is a helpful way to look
at things.
The rules for object reference conversion can be stated as follows:
• An interface type can be converted only to an interface type or to Object. If the new type is an
interface, it must be a superinterface of the old type.
• A class type can be converted to a class type or to an interface type. If converting to a class
type, the new type must be a superclass of the old type. If converting to an interface type, the old
class must implement the interface.
• An array may be converted to the class Object, to the interface Cloneable or Serializable,
or to an array. Only an array of object reference types can be converted to an array, and the
Ben Abdallah Helmi Architect
old element type must be convertible to the new element type.
14
J2EE
1. Tangelo tange = new Tangelo();
2. Citrus cit = tange;
1. Citrus cit = new Citrus();
2. Tangelo tange = cit;//problem
1. Grapefruit g = new Grapefruit();
2. Squeezable squee = g; // No problem
3. Grapefruit g2 = squee; // Error

1. Fruit fruits[];
2. Lemon lemons[];
3. Citrus citruses[] = new
Citrus[10];
4. for (int i = 0; i < 10; i++) {
5. citruses[i] = new Citrus();
6. }
7. fruits = citruses; // No problem
8. lemons = citruses; // Error
Ben Abdallah Helmi Architect
15
J2EE
Object Method-Call Conversion
Fortunately, the rules for method-call conversion of object
reference values are the same as the rules described
earlier for assignment conversion of objects. The general
rule of thumb is that converting to a superclass is
permitted and converting to a subclass is not permitted.
1. Vector myVec = new Vector();
2. Tangelo tange = new Tangelo();
3. myVec.add (tange);

Ben Abdallah Helmi Architect
16
J2EE
Object Reference Casting
• Object reference casting is like primitive casting: by
using a cast, you convince the compiler to let you do
a conversion that otherwise might not be allowed.
• Any kind of conversion that is allowed for
assignments or method calls is allowed for explicit
casting.
• To understand how object casting works, it is
important to understand the difference between
objects and object reference variables.

Ben Abdallah Helmi Architect
17
J2EE
Quite a few rules govern object casting because a large number of obscure cases must be covered.
For the exam, the important rules to remember when casting from Oldtype to Newtype
are as follows:
•

When both Oldtype and Newtype are classes, one class must be a subclass of the other.

•

When both Oldtype and Newtype are arrays, both arrays must contain reference types (not primitives), and it must be legal to cast
an element of Oldtype to an element of Newtype.

•

You can always cast between an interface and a nonfinal object.

Assuming that a desired cast survives compilation, a second check must occur at runtime. The second check determines whether the
class of the object being cast is compatible with the new type. (This check could not be made at compile time, because the object
being cast did not exist then.) Here, compatible means that the class can be converted according to the conversion rules discussed
in the previous two sections. The following rules cover the most common runtime cases:
•

If Newtype is a class, the class of the expression being converted must be Newtype or must inherit from Newtype.

•

If Newtype is an interface, the class of the expression being converted must implement Newtype.

1. Grapefruit g, g1;
2. Citrus c;
3. Tangelo t;
4. g = new Grapefruit(); // Class is Grapefruit
5. c = g; // Legal assignment conversion,
// no cast needed
6. g1 = (Grapefruit)c; // Legal cast
7. t = (Tangelo)c; // Illegal cast
//(throws an exception)

Ben Abdallah Helmi Architect
18
J2EE
1. Grapefruit g, g1;
2. Squeezable s;
3. g = new Grapefruit();
4. s = g; // Convert Grapefruit to Squeezable (OK)
5. g1 = s; // Convert Squeezable to Grapefruit
// (Compile error)
5. g1 = (Grapefruit)s;

1. Grapefruit g[];
2. Squeezable s[];
3. Citrus c[];
4. g = new Grapefruit[500];
5. s = g; // Convert Grapefruit array to
// Squeezable array (OK)
6. c = (Citrus[])s; // Cast Squeezable array to
Citrus
// array (OK)

Ben Abdallah Helmi Architect J2EE
19

More Related Content

PPTX
Ch5 Selection Statements
PPTX
Ch4 Expressions
DOCX
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
PPTX
Ch9 Functions
PPTX
2.dynamic
PPTX
Ch10 Program Organization
PDF
Lecture 2 keyword of C Programming Language
PPTX
5. using variables, data, expressions and constants
Ch5 Selection Statements
Ch4 Expressions
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Ch9 Functions
2.dynamic
Ch10 Program Organization
Lecture 2 keyword of C Programming Language
5. using variables, data, expressions and constants

What's hot (20)

PPT
C language Unit 2 Slides, UPTU C language
PDF
Lecture13 control statementswitch.ppt
PPTX
Cpu-fundamental of C
PPTX
Switch case and looping statement
PDF
Pointers-Computer programming
PDF
The Function Pointer Tutorials
PDF
Notes: Verilog Part 4- Behavioural Modelling
PDF
Functions-Computer programming
PPTX
Computer programming(CP)
DOCX
C# language basics (Visual studio)
PPTX
Javascripts hidden treasures BY - https://geekyants.com/
PDF
Top C Language Interview Questions and Answer
PPTX
Function pointer
PDF
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
PDF
[C++][a] tutorial 2
PDF
12 computer science_notes_ch01_overview_of_cpp
PDF
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
PPT
Csharp4 operators and_casts
PDF
Unequal Equivalence
PDF
Definitions of Functional Programming
C language Unit 2 Slides, UPTU C language
Lecture13 control statementswitch.ppt
Cpu-fundamental of C
Switch case and looping statement
Pointers-Computer programming
The Function Pointer Tutorials
Notes: Verilog Part 4- Behavioural Modelling
Functions-Computer programming
Computer programming(CP)
C# language basics (Visual studio)
Javascripts hidden treasures BY - https://geekyants.com/
Top C Language Interview Questions and Answer
Function pointer
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
[C++][a] tutorial 2
12 computer science_notes_ch01_overview_of_cpp
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Csharp4 operators and_casts
Unequal Equivalence
Definitions of Functional Programming
Ad

Similar to chap4 : Converting and Casting (scjp/ocjp) (20)

PDF
Shivani Chouhan ,BCA ,2nd Year
PPTX
A Case Study on Java. Java Presentation
PDF
Rounit Mathur,BCA 2nd year.
PDF
Nikita Totlani ,BCA 2nd year
PPTX
01 Java Language And OOP PART I
PPT
Ap Power Point Chpt7
PDF
Kajal Gaharwal,BCA 2nd Year
PDF
Pooja Sharma , BCA Third Year
PDF
Deepika Mittal,BCA ,2nd Year
PDF
Kaushal Soni,BCA,2nd Year
PDF
Rakesh Bijawat , BCA Third Year
PDF
Rakesh Bijawat, BCA 2nd Year
PDF
Reshma Kodwani,BCA,2nd Year
PDF
Sudarshan Joshi,BCA 2nd Year
PPTX
OOP - Lecture04 - Variables, DataTypes and TypeConversion.pptx
PDF
Akshay Sharma,BCA,2nd year
PDF
Aanchal Gupta,BCA 2nd year
PDF
Shaikh Mohammad Usman Haider ,BCA 2nd Year
PPTX
Chap1 language fondamentale of java ( scjp /ocjp)
PDF
Pinkle Makhijani ,BCA 2nd Year
Shivani Chouhan ,BCA ,2nd Year
A Case Study on Java. Java Presentation
Rounit Mathur,BCA 2nd year.
Nikita Totlani ,BCA 2nd year
01 Java Language And OOP PART I
Ap Power Point Chpt7
Kajal Gaharwal,BCA 2nd Year
Pooja Sharma , BCA Third Year
Deepika Mittal,BCA ,2nd Year
Kaushal Soni,BCA,2nd Year
Rakesh Bijawat , BCA Third Year
Rakesh Bijawat, BCA 2nd Year
Reshma Kodwani,BCA,2nd Year
Sudarshan Joshi,BCA 2nd Year
OOP - Lecture04 - Variables, DataTypes and TypeConversion.pptx
Akshay Sharma,BCA,2nd year
Aanchal Gupta,BCA 2nd year
Shaikh Mohammad Usman Haider ,BCA 2nd Year
Chap1 language fondamentale of java ( scjp /ocjp)
Pinkle Makhijani ,BCA 2nd Year
Ad

More from It Academy (20)

PPTX
Chapter 12:Understanding Server-Side Technologies
PPTX
Chapter 10:Understanding Java Related Platforms and Integration Technologies
PPTX
Chapter 11:Understanding Client-Side Technologies
PPTX
Chapter 9:Representing Object-Oriented Concepts with UML
PPTX
Chapter8:Understanding Polymorphism
PPTX
Chapter 7:Understanding Class Inheritance
PPTX
Chapter 6:Working with Classes and Their Relationships
PPTX
Chapter 5:Understanding Variable Scope and Class Construction
PPTX
Chapter 4:Object-Oriented Basic Concepts
PPTX
Chapter 3:Programming with Java Operators and Strings
PPTX
Chapter 3
PPTX
Chapter 3 : Programming with Java Operators and Strings
PPTX
Chapter 2 : Programming with Java Statements
PPTX
Chapter 1 :
PPTX
chap 10 : Development (scjp/ocjp)
PPTX
Chap 9 : I/O and Streams (scjp/ocjp)
PPTX
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
PPTX
chap 7 : Threads (scjp/ocjp)
PPTX
chap 6 : Objects and classes (scjp/ocjp)
PPTX
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
Chapter 12:Understanding Server-Side Technologies
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 11:Understanding Client-Side Technologies
Chapter 9:Representing Object-Oriented Concepts with UML
Chapter8:Understanding Polymorphism
Chapter 7:Understanding Class Inheritance
Chapter 6:Working with Classes and Their Relationships
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 4:Object-Oriented Basic Concepts
Chapter 3:Programming with Java Operators and Strings
Chapter 3
Chapter 3 : Programming with Java Operators and Strings
Chapter 2 : Programming with Java Statements
Chapter 1 :
chap 10 : Development (scjp/ocjp)
Chap 9 : I/O and Streams (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
Sports Quiz easy sports quiz sports quiz
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...

chap4 : Converting and Casting (scjp/ocjp)

  • 1. Converting and Casting Ben Abdallah Helmi Architect 1 J2EE
  • 2. Explicit and Implicit Type Changes • Button btn = (Button) (myVector.elementAt(5)); • Of course, the sixth element of the vector must be capable of being treated as a Button Compiletime rules and runtime rules must be observed Ben Abdallah Helmi Architect 2 J2EE
  • 3. Primitives and Conversion • Both primitive values and object references can be converted and cast, so you must consider four general cases: > > Casting of primitives > Conversion of object references > • Conversion of primitives Casting of object references The simplest topic is implicit conversion of primitives. All conversion of primitive data types takes place at compile time; this is the case because all the information needed to determine whether the conversion is legal is available at compile time. • Conversion of a primitive might occur in three contexts or situations: > Assignment > Method call > Arithmetic promotion Ben Abdallah Helmi Architect 3 J2EE
  • 4. Primitive Conversion: Assignmen • A boolean cannot be converted to any other type. • A nonboolean can be converted to another nonboolean type, provided the conversion is a widening conversion • A nonboolean cannot be converted to another nonboolean type if the conversion would be a narrowing conversion Ben Abdallah Helmi Architect 4 J2EE
  • 5. From a byte to a char From a short to a byte or a char From a char to a byte or a short From an int to a byte, a short, or a char From a long to a byte, a short, a char, or an int From a float to a byte, a short, a char, an int, or a long • From a double to a byte, a short, a char, an int, a long, or a float • • • • • • Ben Abdallah Helmi Architect 5 J2EE
  • 6. Assignment Conversion, Narrower Primitives, and Literal Values Java’s assignment conversion rule is sometimes inconvenient when a literal value is assigned to a primitive. By default, a numeric literal is either a double or an int, so the following line of code generates a compiler error: float f = 1.234; The literal value 1.234 is a double, so it cannot be assigned to a float variable. You might assume that assigning a literal int to some narrower integral type (byte, short,or char) would fail to compile in a similar way. For example, it would be reasonable to assume that all of the following lines generate compiler errors: byte b = 1; short s = 2; char c = 3; In fact, all three of these lines compile without error. The reason is that Java relaxes its assignment conversion rule when a literal int value is assigned to a narrower primitive type (byte,short, or char), provided the literal value falls within the legal range of the primitive type. This relaxation of the rule applies only when the assigned value is an integral literal. Thus the second line of the following code will not compile: int i = 12; byte b = i Ben Abdallah Helmi Architect 6 J2EE
  • 7. Primitive Conversion: Method Call 1. float frads; 2. double d; 3. frads = 2.34567f; 4. d = Math.cos(frads); // Pass float to method // that expects double 1. double d = 12.0; 2. Object ob = myVector.elementAt(d); Fortunately, the rule that governs which method-call conversions are permitted is the same rule that governs assignment conversions. Widening conversions (as shown in Figure 4.3) are permitted; narrowing conversions are forbidden Ben Abdallah Helmi Architect 7 J2EE
  • 8. 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("<"); Ben Abdallah Helmi Architect 8 J2EE
  • 9. • For unary operators, two rules apply, depending on the type of the single operand: If the operand is a byte, a short, or a char, it is converted to an int (unless the operator is ++ or --, in which case no conversion happens). Else there is no conversion. • For binary operators, there are four rules, depending on the types of the two operands: If one of the operands is a double, the other operand is converted to a double. Else if one of the operands is a float, the other operand is converted to a float.Else if one of the operands is a long, the other operand is converted to a long. Else both operands are converted to ints. shows Java’s unary and binary arithmetic operators. Ben Abdallah Helmi Architect 9 J2EE
  • 10. Primitives and Casting Casting is explicitly telling Java to make a conversion. A casting operation may widen or narrow its argument. To cast, just precede a value with the parenthesized name of the desired type. For example, the following lines of code cast an int to a double: 1. int i = 5; 2. double d = (double)i; Of course, the cast is not always necessary. The following code, in which the cast has been omitted, would do an assignment conversion on i, with the same result as the previous example: 1. int i = 5; 2. double d = i; Casts are required when you want to perform a narrowing conversion. Such conversion will never be performed implicitly; you have to program an explicit cast to convince the compiler that what you really want is a narrowing conversion. Narrowing runs the risk of losing information the cast tells the compiler that you accept the risk. 1. short s = 259; 2. byte b = (byte)s; // Explicit cast 3. System.out.println("b = " + b); b=3 Ben Abdallah Helmi Architect 10 J2EE
  • 11. 1. int i = 2; 2. double radians; . // Hundreds of . // lines of . // code 600. radians = (double)i; The cast in the last line is not required, but it serves as a good reminder to any readers (including yourself) who might have forgotten the type of radians. Two simple rules govern casting of primitive types: You can cast any non-boolean type to any other non-boolean type. You cannot cast a boolean to any other type; you cannot cast any other type to a boolean. Note that although casting is ordinarily used when narrowing, it is perfectly legal to cast when widening. The cast is unnecessary, but it provides a bit of clarity. Ben Abdallah Helmi Architect 11 J2EE
  • 12. Object Reference Conversion Object reference conversion is more complicated than primitive conversion, because there are more possible combinations of old and new types—and more combinations mean more rules. Reference conversion, like primitive conversion, takes place at compile time, because the compiler has all the information it needs to determine whether the conversion is legal. Later you will see that this is not the case for object casting. The following sections examine object reference assignment, method-call, and casting conversions. Ben Abdallah Helmi Architect 12 J2EE
  • 13. Object Reference Assignment Conversion Object reference assignment conversion happens when you assign an object reference value to a variable of a different type. There are three general kinds of object reference type: • A class type, such as Button or FileWriter • An interface type, such as Cloneable or LayoutManager • An array type, such as int[][] or TextArea[] Generally speaking, assignment conversion of a reference looks like this: 1. Oldtype x = new Oldtype(); 2. Newtype y = x; // reference assignment conversion Ben Abdallah Helmi Architect 13 J2EE
  • 14. Recall that with primitives, conversions were permitted, provided they were widening conversions. The notion of widening does not really apply to references, but a similar principle is at work. In general, object reference conversion is permitted when the direction of the conversion is “up” the inheritance hierarchy; that is, the old type should inherit from the new type. This rule of thumb does not cover all nine cases, but it is a helpful way to look at things. The rules for object reference conversion can be stated as follows: • An interface type can be converted only to an interface type or to Object. If the new type is an interface, it must be a superinterface of the old type. • A class type can be converted to a class type or to an interface type. If converting to a class type, the new type must be a superclass of the old type. If converting to an interface type, the old class must implement the interface. • An array may be converted to the class Object, to the interface Cloneable or Serializable, or to an array. Only an array of object reference types can be converted to an array, and the Ben Abdallah Helmi Architect old element type must be convertible to the new element type. 14 J2EE
  • 15. 1. Tangelo tange = new Tangelo(); 2. Citrus cit = tange; 1. Citrus cit = new Citrus(); 2. Tangelo tange = cit;//problem 1. Grapefruit g = new Grapefruit(); 2. Squeezable squee = g; // No problem 3. Grapefruit g2 = squee; // Error 1. Fruit fruits[]; 2. Lemon lemons[]; 3. Citrus citruses[] = new Citrus[10]; 4. for (int i = 0; i < 10; i++) { 5. citruses[i] = new Citrus(); 6. } 7. fruits = citruses; // No problem 8. lemons = citruses; // Error Ben Abdallah Helmi Architect 15 J2EE
  • 16. Object Method-Call Conversion Fortunately, the rules for method-call conversion of object reference values are the same as the rules described earlier for assignment conversion of objects. The general rule of thumb is that converting to a superclass is permitted and converting to a subclass is not permitted. 1. Vector myVec = new Vector(); 2. Tangelo tange = new Tangelo(); 3. myVec.add (tange); Ben Abdallah Helmi Architect 16 J2EE
  • 17. Object Reference Casting • Object reference casting is like primitive casting: by using a cast, you convince the compiler to let you do a conversion that otherwise might not be allowed. • Any kind of conversion that is allowed for assignments or method calls is allowed for explicit casting. • To understand how object casting works, it is important to understand the difference between objects and object reference variables. Ben Abdallah Helmi Architect 17 J2EE
  • 18. Quite a few rules govern object casting because a large number of obscure cases must be covered. For the exam, the important rules to remember when casting from Oldtype to Newtype are as follows: • When both Oldtype and Newtype are classes, one class must be a subclass of the other. • When both Oldtype and Newtype are arrays, both arrays must contain reference types (not primitives), and it must be legal to cast an element of Oldtype to an element of Newtype. • You can always cast between an interface and a nonfinal object. Assuming that a desired cast survives compilation, a second check must occur at runtime. The second check determines whether the class of the object being cast is compatible with the new type. (This check could not be made at compile time, because the object being cast did not exist then.) Here, compatible means that the class can be converted according to the conversion rules discussed in the previous two sections. The following rules cover the most common runtime cases: • If Newtype is a class, the class of the expression being converted must be Newtype or must inherit from Newtype. • If Newtype is an interface, the class of the expression being converted must implement Newtype. 1. Grapefruit g, g1; 2. Citrus c; 3. Tangelo t; 4. g = new Grapefruit(); // Class is Grapefruit 5. c = g; // Legal assignment conversion, // no cast needed 6. g1 = (Grapefruit)c; // Legal cast 7. t = (Tangelo)c; // Illegal cast //(throws an exception) Ben Abdallah Helmi Architect 18 J2EE
  • 19. 1. Grapefruit g, g1; 2. Squeezable s; 3. g = new Grapefruit(); 4. s = g; // Convert Grapefruit to Squeezable (OK) 5. g1 = s; // Convert Squeezable to Grapefruit // (Compile error) 5. g1 = (Grapefruit)s; 1. Grapefruit g[]; 2. Squeezable s[]; 3. Citrus c[]; 4. g = new Grapefruit[500]; 5. s = g; // Convert Grapefruit array to // Squeezable array (OK) 6. c = (Citrus[])s; // Cast Squeezable array to Citrus // array (OK) Ben Abdallah Helmi Architect J2EE 19