You are on page 1of 5

Fundamentals of the Java Programming Language

Lesson IV

Property of Airos Education


Copyright 2018 Airos Education
www.airos.co.za
Lesson IV (part 2): Promotion and Type Casting

In Java, promotion and type casting are used to convert the data type of an argument or
operand. Promotion is an implicit type conversion. In other words, the Java programming
language converts one type to another without any intervention from the programmer. On
the other hand, casting is an explicit type conversion. This means that the programmer uses
special syntax (words and structure) to indicate that a type conversion is requested.

Promotion

Promotion, also referred to as implicit casting or automatic type conversion, happens


implicitly by the JVM when both data types involved are compatible and the target type is
larger than the source type. In other words, a data type can only be promoted to a type that
is higher in size. For example, an integer, which is 4 bytes in size, can be converted to a
double, which is 8 bytes. However, a double cannot be converted into an integer because
the size of an integer is smaller than that of a double.

Let’s consider an example where promotion occurs:

double​ result ​=​ ​8.2​ ​/​ ​2;

The division operator in the example requires two operands. In other words, we need to
divide one operand by another. This means that the division operator is a binary operator i.e.
it requires two arguments e.g. 8.2 / 2 (where 8.2 and 2 are both arguments). Now, in Java,
both operands of a binary operator need to have the same type, however, in our example,
we have a double operand (8.2) divided by an integer operand (2). Thus, the Java
programming language is going to convert the operand of type integer, implicitly. This means
that we did not indicate to the compiler that the integer should be converted to a double. This
is automatically conducted by the compiler, so that the division can take place between two
operands of the same type. Hence, the integer 2 is converted into a double 2.0. If you were
to print the result to the console, the output will be of type double:

System​.​out​.​println​(​result​);​ ​//prints 4.1

The primitive data types can be arranged hierarchically, as follows:

Type Size Valid Promotion

double 8 bytes

float 4 bytes double

long 8 bytes double, float

int 4 bytes double, float, long

char 2 bytes double, float, long, int

Property of Airos Education


Copyright 2018 Airos Education
www.airos.co.za
1
short 2 bytes double, float, long, int (not
char)

byte 1 byte double, float, long, int (not


char), short

boolean cannot be promoted/casted


Table 1: Size and valid promotions of the primitive data types

From the table, you can see that a float can be implicitly promoted to a double, whereas, a
long can be promoted to either a double or a float, and so on.

Figure 1:Conversion from one data type to another data type

Property of Airos Education


Copyright 2018 Airos Education
www.airos.co.za
2
Type Casting

As you know from the previous section, a data type of a higher size cannot be implicitly
converted to a data type of a lower size by the JVM. Therefore, if we wish to convert, for
example, a double to an integer, then that will require explicit casting. In other words, we will
have to use special syntax to indicate to the compiler that we want to convert the integer into
a double. So, how do we do this? Well, intuitively, you may think that the following will
suffice:

​ ​ ​5.7​;​ ​//8 bytes


double​ height =
int​ heightInt =​ ​ height​;​ ​//4 bytes

However, that method will raise a compilation error because we have two different types and
there is no promotion from a double to an integer (as indicated by the table). So, why can’t
the JVM do the opposite and cast the integer to a double? Well, in our example, we’re
declaring and assigning a value to a new variable called ​heightInt​. The variable ​heightInt
cannot be changed with promotion or casting, however, what can be changed is the type of
the value that we assign to it. Hence, we will have to explicitly type cast the value that is
assigned to ​heightInt​, as follows:

int​ heightInt ​=​ ​int​(​height​);

The double ​height is explicitly converted to an int ​heightInt​. The rule of thumb to remember is
that the same data type should exist on both sides of the assignment operator.

Boolean Casting

As you may have already figured, a boolean value cannot be assigned to another data type.
With the exception of boolean, all primitive data types can be explicitly or implicitly converted
to another type. Therefore, we say that boolean is incompatible for conversion. The best we
can do is to assign another boolean value to a boolean:

boolean​ t ​=​ ​true;


int​ i ​=​ t​;​ ​//compilation error

boolean​ f ​=​ ​false;


int​ i ​=​ ​int​(​f​);​ ​//compilation error

//change a boolean type from false to true

boolean​ f ​=​ ​false​;


System​.​out​.​println​(​f​);​ ​//prints false

f ​=​ ​true;
System​.​out​.​println​(​f​);​ ​//prints true

Property of Airos Education


Copyright 2018 Airos Education
www.airos.co.za
3
Formative Assessment

1. In your own words, describe the differences between promotion and type casting.
2. For the following code samples, state whether or not there will be an error, giving a
reason for your answer:

a. int​ a ​=​ ​6 ​/ ​2;

b. int​ c ​=​ ​8 ​/ ​2.5;

c. double​ e ​=​ ​6.5​;


int​ f ​=​ ​(​int​)​e;

d. double​ b ​=​ ​6.5 ​/ ​4.5​;

e. long​ length ​=​ ​14L​;


float​ i ​=​ ​(​float​)​length;

f. long​ c ​=​ ​16L;


float​ d ​=​ ​0.2F;

System​.​out​.​println​(​"c * d = "​ ​+ ​c ​*​ d​);

Property of Airos Education


Copyright 2018 Airos Education
www.airos.co.za
4

You might also like