Search code examples
typescastingcompiler-constructionimplicit-conversion

Compiler Design - How does does automatic type casting occur when multiple cast levels away


enter image description here

To convert from byte to short, would I be right to assume there's some kind of method being called in the background with the signature:

short byte2Short(byte b);

But what about the situation where a byte has to be converted to a long? I.e. in the conversion diagram they are not connected directly by a arrow.

Is there a specific method that converts a byte into a long such as:

long byte2Long(byte b);

Or instead does it call a chain of methods that converts the byte as follows:

  1. short byte2Short(byte b);

  2. int short2Int(short s);

  3. long int2Long(int i);

So essentially my question is, if converting from a byte to a long, is the conversion direct like: byte -> long

Or is it indirect like: byte -> short -> int -> long


Solution

  • The conversion happens directly, i.e., byte -> long.

    C implicit conversions

    If the types have the same signedness (both signed or both unsigned), the operand whose type has the lesser conversion rank1 is implicitly converted2 to the other type.