..


Sponsored Links

The Java numeric promotion

Article written by Damiano Verda
Page 1 of 2

What is numeric promotion? This is a very common operation in a program and corresponds to an implicit conversion of operands (for example, the addend) associated with a numeric operator (eg "+" indicates that the addition).

This conversion is necessary whenever the operands do not match exactly the type of data that is expected for the operation. In some cases, the conversion must be explicit (for example using a cast ) and is therefore the responsibility of the programmer.

In other cases, especially in the case of more advanced programming languages ​​and distributed (hereafter we will refer in particular to the Java language), the conversion is done automatically, thus implicitly. In this case it is possible to speak of numeric promotion.

We can distinguish two main types of numeric promotion: Unary numeric promotion and binary numeric promotion, which differ substantially in the nature of the arithmetic involved in the operation (unary or binary). Let's look at both strands, taking care to also provide some brief examples of Java code.

Unary numeric promotion

Let's see an example:






 byte b = 2;







 int a [] = new int [b];

           





 Character c = '\ u0001';







 to [c] = 1;

                       





 a [0] = c;

  

In this case, though very simple, we can identify three examples of Unary numeric promotion. The first is education int a [] = new int [b]. The size of the 'fact array should be represented by an integer (int variable), while b is a variable of type byte.

Afterwards, let us reflect on education [c] =- 1. In this case it is passed as an index of 'arrays, once again, not a parameter of type integer, but (in this case) type character. Similarly, the next statement a [0] =- c 0 gives the position of the 'array to a value of type int and not character.

We observe first that it is associated with conversion to 'assignment operator ("="), therefore a unary operator, that is associated with only one operand. Here is why we can speak of Unary numeric promotion. How the compiler handles the conversions implicitly associated with the steps?

In all three cases, the conversion can be decomposed into two elementary steps. First is a conversion type unboxing, ie the data (in this case of type Byte or Character) is converted into the corresponding primitive data type (char byte, respectively).

At this point we proceed to a second phase of conversion in all three cases under consideration is a widening primitive conversion. In short, there is no loss of information and indeed, the new data type you choose (int) is overabundant compared to store information (contained in the variable b, or c of type Byte, character type). For this reason the compiler can perform the conversion yourself.

We emphasize it as the only real conversions that can be performed implicitly in the context of numeric promotion are those mentioned. There is in fact also the possibility of a type conversion identity, but maintains the data type of the variable in question and is therefore of interest mainly theoretical.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links