answersLogoWhite

0

Type PromotionIn the aforementioned example System.out.println(3 + 4), the expression 3 + 4 is evaluated to 7 and then converted automatically, at runtime, to a String value which is whatprintln expects. This automatic conversion is a somewhat special case of type promotion.

Type promotion is an automatic type conversion from a "lesser" base type to a "greater" one or from any type to String via the toString() method.

A promotion from on base type to another may occur in an arithmetic expression:

Example:double x = 3.14 + 10; /* 10 is promoted to a double */ double y = 12; /* 12 is promoted to a double then assigned */

As mentioned earlier, type promotion occurs from a "lesser" type to a "greater" one where a type is "less" than another if it offers smaller precision. In our previous example, an int offers less precision than a double thus an int value can be promoted to a double as necessary. For the same reason a double cannot be promoted to an int. If an double is used where an int is expected, Java will generate an error:

Example:int z = 2.4; Compile-time error: possible loss of precision
User Avatar

Wiki User

13y ago

What else can I help you with?