Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

Difference between Type casting and Up/Down casting in inheritance [Java]?

We know the following:: (Edit 20210101_0059 EST)

public class Main {
  public static void main(String[] args) {
    int myInt = 9;
    double myDouble = (double) myInt; // casting: int to double 
  }
}

Type casting is when you assign a value of one primitive data type to another type.

Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double https://www.w3schools.com/java/java_type_casting.asp


Question::

What is the difference between Type casting and Upcasting / Downcasting? How Type casting works?

Is that upcasting in the example? If so, does it mean integers inherit from double? (which I don't think so)



Update Below 20210101_0059~0154 EST

I change the line

double myDouble = myInt; // casting: int to double
// change to >>
double myDouble = (double) myInt; // casting: int to double

Because my intention was to talk about casting. That mistake led people to start to talk about type conversion instead of casting.


By now, base on the answers, I know there is no up/downcasting in Java Language Specification. And I briefly looked through 5.5. Casting Contexts & 5.5.1. Reference Type Casting

Question::

So, what exactly is a casting? is "casting" basically just "type conversion"?

And there are many types of casting (/ type conversion) right?


Questions (for confirmation)::

1. And so, casting does not necessary exist between superclass and subclass (ie: casting does not only exist in 2 class with inheritance relationship), right?

2. Usually, when we talk about up/downcasting, we are using it in "Reference Type Casting". But in our case double myDouble = (double) myInt; there is no up/downcasting. And, in both cases, the castings (Up/Down casting & int-double casting) are just "type conversion". Right?

3. In short, the only difference between them are just:

Type casting -- a general term for casting.

Up/Down casting -- a term only used in "Reference Type Casting", which involves inheritance.

casting "byte short char int long float double" (eg: double myDouble = (double) myInt;) -- (just a different type of casting that is not used in "Reference Type Casting").

Right?



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Java Language Specification does not mention "upcasting" or "downcasting" at all, so as far as the language specification is concerned, there are no such thing as upcasting or downcasting.

Type casting is when you assign a value of one primitive data type to another type.

This is not true according to the specification. The spec specifies a cast operator that looks like this:

(some type) some expression

e.g.

(float)someDouble

The spec also specifies that this is a casting context.

What W3Schools meant by "type casting", the spec calls a "conversion". Conversions occurs in contexts. And by "upcasting" and "downcasting", W3Schools probably meant "widening conversion" and "narrowing conversion".

This:

double myDouble = myInt;

is a conversion from int to double (one of the many widening primitive conversions), occurring in an assignment context. No casts were involved.

Compare that to an actual cast:

(float)myDouble

This is a conversion from double to float (one of the many narrowing primitive conversions) occurring in a casting context.

And no, primitive types like int, double, float etc can't inherit from anything. These primitive conversions are "hardcoded"/mandated by the spec, not as a consequence of "int inherits from double" or anything like that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...