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?