aspose file tools*
The moose likes Beginning Java and the fly likes Is This a Widening or an Upcast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is This a Widening or an Upcast?" Watch "Is This a Widening or an Upcast?" New topic
Author

Is This a Widening or an Upcast?

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
I have seen this used in the SCJP Study Guide both as an example of upcasting and of widening. Which one is it? Is Dog being implicilty upcast or widened to Animal?

Thank you,
-Russ

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

im not sure what your question is. because Dog extends Animal you can pass a Dog when an Animal is expected in your go method.


SCJP
Visit my download page
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Thank you. I am aware that I can pass a Dog when an Animal is expected (that's polymorphism in action). My question is which method the compiler uses to accomplish this. Upcasting or Widening.

-Russ
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

I googled "java widening" and found the Java Language Specification where it talked about "widening primitive conversions". Those aren't primitives.

Does that help? Of course, I don't understand what problem prompted you to ask that question.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Hi,
A bit of googling helped here. This is what I understood, hope it will help.

Widening : This refers only to primitive data types. Check out this link http://www.java2s.com/Tutorial/Java/0040__Data-Type/TheWideningConversion.htm
or in short Widening is permitted in the following cases :

The widening conversion is permitted in the following cases:
byte to short, int, long, float, or double
Short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double

So we have widening as opposed to narrowing, the latter requires an explicit cast.

Upcasting and Downcasting - Refers to using References in inheritance structures.
Just check the diagram from : http://forum.codecall.net/java-tutorials/20719-upcasting-downcasting.html

The example you have, to the best of my knowledge is definately an upcast



A TubeBulb May light slowely... But it lights the Brightest..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Palash Nandi wrote: . . . A bit of googling helped here. . . .
I am afraid the first link didn’t help at all. It has bits missing. Your second link is more useful, particularly when it says that casting reference types is different from casting primitives. It would have been a good idea to use a different word from “cast” for reference types.

Widening can be applied to reference types, as you will find if you read the Java Language Specification (JLS). You can see the other types of conversion in the same section of the JLS.

The JLS doesn’t use the term upcast, so we shall presume the correct term is widening conversion of a reference type.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Campbell Ritchie wrote:
The JLS doesn’t use the term upcast, so we shall presume the correct term is widening conversion of a reference type.


JLS is always a very good resource Ritchie. I just saw that it neither uses upcast nor downcast for anything. Only cast is used.
Though conversion is used plenty of times and many types of them

So i think...


The right statement would be

This is a cast which causes a narrowing conversion

What say ? i think i nailed it



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Palash Nandi wrote: . . . What say ? i think i nailed it
What you showed there is a narrowing primitive conversion.

Primitive casts are totally different from reference casts, which is what the original question was about.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Campbell Ritchie wrote:What you showed there is a narrowing primitive conversion.


Yes. I saw JLS, you are referring to this


In every conversion context, only certain specific conversions are permitted. For convenience of description, the specific conversions that are possible in the Java programming language are grouped into several broad categories:

Identity conversions
Widening primitive conversions
Narrowing primitive conversions
Widening reference conversions
Narrowing reference conversions
Boxing conversions
Unboxing conversions
Unchecked conversions
Capture conversions
String conversions
Value set conversions



Campbell Ritchie wrote:Primitive casts are totally different from reference casts, which is what the original question was about.


I think Rus asked if the example was of type widening or upcasting : Then the answer is both. Since widening and upcasting can refer to reference as well as primitive. Though the word upcast is never used in the JLS.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Palash Nandi wrote: . . . I think Rus asked if the example was of type widening or upcasting : Then the answer is both. Since widening and upcasting can refer to reference as well as primitive. Though the word upcast is never used in the JLS.
I have never heard anybody call a widening primitive conversion an upcast before. I have only heard upcast and downcast used of reference types. Otherwise: agree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is This a Widening or an Upcast?