Lesson 4 Part 2
Lesson 4 Part 2
Lesson IV
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
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:
double 8 bytes
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.
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:
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:
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:
f = true;
System.out.println(f); //prints true
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: