Internal Data Representation Overview l Internal Representation of

  • Slides: 11
Download presentation
Internal Data Representation Overview l Internal Representation of Integers. l Overflow and Underflow l

Internal Data Representation Overview l Internal Representation of Integers. l Overflow and Underflow l Representing Floating Point Numbers l Representing Characters l Conversions l Arithmetic and Casting 1

Internal Data Representation l l l This part concentrates on: • Internal data representation.

Internal Data Representation l l l This part concentrates on: • Internal data representation. • Conversions between one data type and another. We discussed earlier that every piece of information stored on a computer is represented as binary values. What is represented by the following binary string? 01100001001010 There are four types of integers in Java, each providing a different number of bits to store the value. Each has a sign bit. If it is 1, the number is negative; if it is 0, the number is positive. byte s short s int s long s 7 bits 15 bits 31 bits 63 bits 2

Representation in 2’s Complement l l l l Integers are stored in signed two's

Representation in 2’s Complement l l l l Integers are stored in signed two's complement format. A positive value is a straightforward binary number. A negative value is represented by inverting all of the bits of the corresponding positive value, then adding 1. To "decode" a negative value, invert all of the bits and add 1. Using two's complement makes internal arithmetic processing easier. The number 25 is represented in 8 bits (byte) as: 00011001 To represent -25, first invert all of the bits 11100110 then add 1: 11100111 l Note that the sign bit reversed, indicating the number is negative. 3

Overflow and Underflow l Storing numeric values in a fixed storage size can lead

Overflow and Underflow l Storing numeric values in a fixed storage size can lead to overflow and underflow problems. l Overflow occurs when a number grows too large to fit in its allocated space. l Underflow occurs when a number shrinks too small to fit in its allocated space. 4

Representing Floating Point Values l l A decimal (base 10) floating point value can

Representing Floating Point Values l l A decimal (base 10) floating point value can be defined by the following equation sign * mantissa * 10 exponent where » sign is either 1 or -1. » mantissa is a positive value that represents the significant digits of the number. » exponent is a value that indicates how the decimal point is shifted relative to the mantissa. l l l For example, the number -843. 977 can be represented by: -1 * 843977 * 10 -3 Floating point numbers can be represented in binary the same way, except that the mantissa is a binary number and the base is 2 instead of 10: sign * mantissa * 2 exponent Floating point values are stored by storing each of these components in the space allotted. 1 float sign 1 double 8 23 exponent mantissa 11 sign exponent 52 mantissa 5

Representing Characters l l As described earlier, characters are represented according to the Unicode

Representing Characters l l As described earlier, characters are represented according to the Unicode Character Set. The character set matches a unique number to each character to be represented. Storing the character is therefore as simple as storing the binary version of the number that represents it. For example, the character 'z' has the Unicode value 122, which is represented in 16 bits as: 000001111010 Because they are stored as numbers, Java lets you perform some arithmetic processing on characters. l For example, because 'A' is stored as Unicode value 65, the statement: char ch = 'A' + 5; will store the character 'F' in the variable ch (Unicode value 70). l This relationship is occasionally helpful. l 6

Conversion l Each data value and variable is associated with a particular data type.

Conversion l Each data value and variable is associated with a particular data type. l It is sometimes necessary to convert a value of one data type to another. l Not all conversions are possible. For example, boolean values cannot be converted to any other type and vice versa. l Even if a conversion is possible, we need to be careful that information is not lost in the process. 7

Widening Conversions l Widening conversions are generally safe because they go from a smaller

Widening Conversions l Widening conversions are generally safe because they go from a smaller data space to a larger one. l The widening conversions are: From To byte short char int long float short, int, long, float, or double float or double 8

Narrowing Conversions l Narrowing conversions are more dangerous because they usually go from a

Narrowing Conversions l Narrowing conversions are more dangerous because they usually go from a smaller data space to a larger one. l The narrowing conversions are: From To byte short char int long float double char byte or short byte, short, or char byte, short, char, or int byte, short, char, int or long byte, short, char, int, long, or float 9

Performing Conversion l l In Java, conversion between one data type and another can

Performing Conversion l l In Java, conversion between one data type and another can occur three ways. Assignment conversion: When a value of one type is assigned to a variable of another type. Arithmetic promotion: Occurs automatically when operators modify the types of their operands. Casting: An operator that forces a value to another type. Example: if money is a float variable and dollars is an int variable (storing 82), then: money = dollars; converts the value 82 to 82. 0 when it is stored. l The value in dollars is not actually changed!!! l l Only widening conversions are permitted through assignment. Assignment conversion can also take place when passing parameters (which is a form of assignment). 10

Arithmetic and Casting Arithmetic Promotion l Certain operators require consistent types for their operands.

Arithmetic and Casting Arithmetic Promotion l Certain operators require consistent types for their operands. l For example, if sum is a float variable and count is an int variable, then the statement: result = sum / count; internally converts the value in count to a float then performs the division, producing a floating point result. The value in count is not changed Casting l A cast is an operator that is specified by a type name in parentheses. l It is placed in front of the value to be converted. l The following example truncates the fractional part of the floating point value in money and stores the integer portion in dollars: dollars = (int) money; l The value in money is not changed!!! l If a conversion is possible, it can be done through a cast. 11