- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
5.5. Numeric promotions
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:
public class Char {
public static void main(String[] args) {
char c = 'a';
int kod = c;
System.out.println("Unicode value of " + c + " = " + kod);
kod = 77;
c = (char) kod;
System.out.println("Unicode value of " + c + " = " + kod);
// sometimes the widening conversion must be used explicitly
System.out.println("Unicode value of " + '*' + " = " + (int) '*');
// the narrowing conversions must always be used explicitly
System.out.println("Unicode value of " + (char) 66 + " = " + 66);
}
}
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:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
