Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is This a Widening or an Upcast?

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic