Читайте также: |
|
In general, a value of the type B cannot be assigned to a variable of the type A. Thus, the following causes compile-time error:
int a = "PJWSTK";
because the variable a is of the type int, whereas the expression "PJWSTK" is a string literal of the type String.
However, it would be unwise to apply the above rule to numeric types. Variables of numeric types are all numbers, so we would like to treat them similarly regardless of their types.
The modification of the type of a variable is called conversion or promotion (in arithmetical operations).
The conversions among primitive types which cannot lead to any loss of information represented by the expression's value are performed implicitly.
For example, to a variable of the type double the value of the type int can be assigned without an error:
int a = 10;
double d = a;
Indeed, values of the type int (integer numbers) can be represented by the values of the type double (real numbers).
Such conversions are called widening primitive conversions, because "narrower" types are converted to "wider" types (able to represent more values).
On the contrary, the assignment:
double d = 10.1;
...
int a = d;
causes compile-time error. The assignment of a real number to an integer results in loss of information about its fractional part.
Such conversions are called narrowing primitive conversions and are possible only when the cast operator is explicitly used. The conversions involving the cast operator are called casting conversions. They must be specified explicitly to tell the compiler that the programmer consciously forces the conversion of types which may lead to loss of information and that it is not an accidental error in the program.
The syntax of the cast operator is:
(type_name) expression
where type_name - the type to which the actual type of expression is converted
Thus, the previous example should be written as follows:
double d = 10.9;
...
int a = (int) d;
This form is accepted by the compiler.
It is worth noting that such a conversion may result in loss of precision. The value of the variable a after the assignment is 10. Thus, its fractional part was lost.
Conversions are often performed on characters (of the type char) and their Unicode values (which may occur to be of the type int).
Here is a test program that includes examples of conversions on characters:
Unicode value of a = 97
Unicode value of M = 77
Unicode value of * = 42
Unicode value of B = 66
which prints:
The conversions which are applied implicitly to the operands of arithmetic operators are called numeric promotions.
The numeric promotion is the conversion of the operands of a numeric operator to a common type so that an operation can be performed.
The question what is the common type answers the following rule:
Binary numeric promotions (for binary operators) are performed as follows:
Summary
The lecture introduced:
This knowledge enables us to write simple programs performing various calculations correctly.
However, proper declarations of variables and use of literals will become important later when our programs become more complex.
Exercises
Lecture 6
Objects
Programming in Java deals with objects. The notion of object was introduced in lecture 4. Now we want to describe how objects are represented in Java.
Дата добавления: 2015-11-16; просмотров: 63 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
More on operators and expressions | | | Objects and references |