Language Q: widening and narrowing conversions and casting

J

jeffc

Reading Mughal "Programmer's Guide to Java Certification". It talks about
widening and narrowing conversions of primitive data types. Widening is OK
(e.g. int to float). Narrowing causes loss of information. This makes sense.

Now the confusing part, wrt reference types. It also calls upcasting a widening
conversion, and downcasting a narrowing conversion. This makes sense from the
perspective that going up the class hierarchy is usually fine, but going down is
usually not. What doesn't make sense is the "widening/narrowing" terminology,
and the loss of information. You are actually widening going down the
hierarchy, in terms of data size. You are also losing information, in a sense,
by going up the hierarchy. (After upcasting, you no longer have access to the
class extensions, so your object is effectively smaller, not larger.

Comments?
 
E

Eric Sosman

jeffc said:
Reading Mughal "Programmer's Guide to Java Certification". It talks about
widening and narrowing conversions of primitive data types. Widening is OK
(e.g. int to float). Narrowing causes loss of information. This makes sense.

Now the confusing part, wrt reference types. It also calls upcasting a widening
conversion, and downcasting a narrowing conversion. This makes sense from the
perspective that going up the class hierarchy is usually fine, but going down is
usually not. What doesn't make sense is the "widening/narrowing" terminology,
and the loss of information. You are actually widening going down the
hierarchy, in terms of data size. You are also losing information, in a sense,
by going up the hierarchy. (After upcasting, you no longer have access to the
class extensions, so your object is effectively smaller, not larger.

Comments?

Comment: IMHO, "widening" and "narrowing" are the terms
at fault. They do not even describe conversion between
primitive types correctly -- for example, both int-to-float
and float-to-int can lose information, so which type should
be called "wider?" When this already sloppy terminology is
applied to reference variables, it loses what little sense
it had in the first place. Don't waste time trying to stuff
sense back into it.
 
C

Chris Smith

jeffc said:
Reading Mughal "Programmer's Guide to Java Certification". It talks about
widening and narrowing conversions of primitive data types. Widening is OK
(e.g. int to float). Narrowing causes loss of information. This makes sense.

Now the confusing part, wrt reference types. It also calls upcasting a widening
conversion, and downcasting a narrowing conversion. This makes sense from the
perspective that going up the class hierarchy is usually fine, but going down is
usually not. What doesn't make sense is the "widening/narrowing" terminology,
and the loss of information. You are actually widening going down the
hierarchy, in terms of data size. You are also losing information, in a sense,
by going up the hierarchy. (After upcasting, you no longer have access to the
class extensions, so your object is effectively smaller, not larger.

To put it bluntly, a conversion from int to short doesn't "lose
information" either. In each given instance, it either works
splendidly, or it basically gives you garbage as a result (deterministic
garbage, but garbage nonetheless for practically all purposes) So
losing information is, from the start, a poor way to put it.

Instead, think of these terms as "narrowing" and "widening" the range of
possible values. For example, the set of possible values for int
includes such numbers as 0, 15, 700, -49000, or 592000. The set of
values of short is narrower (of the original examples, only 0, 15 and
700 are valid). Similary, the range of possible values for Object
includes all of "17", Integer.valueOf(4), and new Date(), whereas the
type String defines a narrower range, including only "17" out of the
three examples above.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

jeffc

Eric Sosman said:
Comment: IMHO, "widening" and "narrowing" are the terms
at fault. They do not even describe conversion between
primitive types correctly -- for example, both int-to-float
and float-to-int can lose information, so which type should
be called "wider?" When this already sloppy terminology is
applied to reference variables, it loses what little sense
it had in the first place. Don't waste time trying to stuff
sense back into it.

I appreciate that sort of comment because it does help with my understanding. I
didn't mention this, but the main reason I was asking was for the certification
exam.... I know how upcasting and downcasting work reasonably well, but that is
not always the same thing as giving answers they want to hear :)
 
J

jeffc

Chris Smith said:
Instead, think of these terms as "narrowing" and "widening" the range of
possible values.

Putting it that way does seem to work well for both primitive and reference
types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top