### Conversion of values from different data types

• What is a "data type" ?

• Data type:

 Data type = an encoding scheme to represent a set of facts

Example: encoding scheme for the marital status

 0 = single 1 = married 2 = divorsed 3 = widowed

• Encoding schemes for data types:

• Some encoding schemes are more complex than others

Example:

• To encode floating point numbers, we use an encoding scheme that consists of 2 numbers:

 The first number encodes the mantissa ("decimal part") of the floating point number The second number encodes the exponent of the floating point number

Demo: see how a floating point number is encoded

• Converting values between different data types

• Converting a value of one data type into another data type consist of:

 Determining the representation of the same fact in the other data type.

Example:

• The representation for the number 1 in integer is:

 ``` 00000000 00000000 00000000 00000001 (binary) ```

• The representation for the floating point) number 1.0 in float is:

 ``` 00111111 10000000 00000000 00000000 (binary) ```

(Use the applet above to verify)

• When you convert the int represention for 1 into the float representation, using this statement f = i in the following program:

 ``` int i = 1; // i will contain: // 00000000 00000000 00000000 00000001 (binary) float f; f = i; // Converts int represetation to float representation // f will contain: // 00111111 10000000 00000000 00000000 (binary) ```

You will instruct the computer circuitry will perform the conversion between the different representations !

• Converting between related data types

• Fact:

 Every programming language that has been designed, allows the user to convert the value between related data types

Example:

• byte short

 ``` byte repr. of the value 1 short repr. of the value 1 00000001 0000000000000001 ```

Illustrated:

• Note:

• Converting a value between integer types (such as: byte, short, int, and long) is relatively easy

(You will learn more in CS255)

• Some value conversions are very complicated and electronic engineers must make special computer circuits inside the CPU of the computer to perform the conversion

Example: byte float

 ``` byte repr. of the value 1 float repr. of the value 1 00000001 00111111100000000000000000000000 ```

Illustrated:

Applet that let you see the IEEE 754 encoding (representation) of float values:

(Original contributor: click here )

• Converting between unrelated data types

• Example of unrelated data types:

 int boolean

• Fact:

 Some programming languages allows you to convert between unrelated data types While other programming languages may forbid it....

• Java disallows the conversion between unrelated data types

Example:

 ``` public class Test1 { public static void man(String[] args) { int i; boolean b = true; i = (int) b; // NOT allowed !! } } ```

Compile message:

 ``` > javac Test1.java Test1.java:8: inconvertible types found : boolean required: int i = (int) b; ^ 1 error ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory Compile with java Test1.java and observe the compiler message.

• User-defined data types

• Recall:

 User-defined data type in Java is defined using the class concept

Examples:

 GeometricObject is a user-defined data type           Circle is a user-defined data type

• "Converting" between user-defined data types (classes)

• Fact:

 In general, Java does not allow the assignment between variables of different class

• Example:

 ``` class A { int i; } class B { int i; } public class Test2 { public static void main(String[] args) { A x = new A(); B y; y = (B) x; // Illegal !!! } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac Test2.java (Observe the compiler error message)

• "Converting" between base class and derived class types

• Fact:

 The base class and its derived class are "related" to each other !!!! Java (in fact: all Object Oriented Programming languages) allows you to "convert" between a base class type and its derived class type

Example:

• The following assignment statements may be permitted (restrictions apply !!) in Java:

 ``` class GeometricObject { .... } class Circle extends GeometricObject { .... } public class Test3 { public static void main(String[] args) { GeometricObject x = new GeometricObject(); Circle y; x = y; // Always permitted y = (Circle) x; // May be permitted... } } ```

• Note:

• I put the word "convert" between quotes ".....", because:

 There is no literal conversion taking place. I.e., there is no change in the encoding

• The phrase "converting between the base/derived class" just means:

 Assigning a (reference) value of a base/derived class to a variable of a derived/base class

• What happens when you assigns a reference value (address)

• Assigning a reference value (address) of a base/derived class to a variable of a derived/base class has no effect on the reference value (address)

Illustrated:

I.e.: the reference value (address) is unchanged

• Summary of the effect of this assignment:

 Assigning a reference value (address) of a derived class to a variable of a base class will limit the instance variables and methods that you can access Assigning a reference value (address) of a base class to a variable of a derived class will enlarge the instance variables and methods that you can access

We will discuss the effects in more details next.