Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier Type Block Address width int 8 Note: No initialization is involved; there is only an association of a name with an address. width 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 32 Integers vs. Reals Integers (byte, short, int, long): -3, -2, -1, 0, 1, 2, 3, Reals (double, float) include values with a decimal point; e.g., π 3.14579 e 2.71828 etc. 33 1
Reals (format, storage, range) Format Formatted according to the IEEE-754 standard for floating point arithmetic Includes a fractional part and a power (the details needn t concern us here) Storage float 4 bytes double 8 bytes Range float ±10 38 with 7 significant digits double ±10 308 with 15 significant digits 34 Real Examples double x; double interestrate = 1.5; float z = -1.1f; double abc = 3.4E-5; Float literal (default is double) Same as 0.000034 35 2
What happens if Special Cases Division by zero Integers: throws an arithmetic exception Reals: assigns a fictitious value, NaN ( not a number ) Out of range result Integers: range is treated as circular Reals: assigns a fictitious value, Infinity 36 The Type boolean Stores the result of a condition Has only two possible values, true or false true and false are reserved words Boolean variables are not integers 37 3
The Character Type char A char is a letter, digit, or symbol Stores the code, not the typeface The codes for English use ASCII 1 char is thus an (unsigned) integer type Numeric coding of characters uses the Unicode character set Unicode has 64K codes (next three slides) 1 ASCII codes are the first 256 entries in the Unicode character set. Try Wikipedia for more details. 38 Figure A.1 39 4
Figure A.2 40 Figure A.3 41 5
Character literals are recognized by single quotes surrounding a character, e.g., 'A' Special characters, such a single quote itself, are represented as literals using escape sequences Character Literals 42 Primitive and Non-primitive Types The eight types just introduced are short, byte, int, long, float, double, boolean, and char These are Java s primitive types They are internally supported Java also supports non-primitive types (class, interface, array) The most common non-primitive type is the String class (which supports literals) String literals: collection of characters enclosed in double quotes (e.g., "hello world") 43 6
Summary of Java s Primitive Types see next slide 44 N U M B E R PRIMITIVE TYPES I N T E G E R Type Size (bytes) Approximate Range min max S.D. S byte 1-128 +127?- I G short 2-32,768 +32,767?- N E int 4-2 10 9 +2 10 9?- D long 8-9 10 18 +9 10 18?- UNSIGNED char 2 0 65,535?- R E SINGLE float 4 +3.4 10 38 +3.4 10 38 7 A L DOUBLE double 8-1.7 10 308 +1.7 10 308 15 BOOLEAN boolean 1 true/false N/A - 45 7
Topics Anatomy of a program The declaration statement The assignment statement 46 Assignment Statement The statement (from Area.java) width = 8; is of the general form name = value; Pre-declared and in-scope Type can hold RHS Content will be overwritten Literal Name, or Expression Note: RHS = right-hand side, LHS = left-hand side 47 8
Examples int quantity; quantity = 25; int quantity = 25; int stock = quantity; int quantity = 25; char grade = B ; boolean isfound = false; double intrate = 1.25; int stock = 100; int order = 15; int total = order + stock; Declaration Assignment Declaration and assignment combined Name of variable on RHS Expression on RHS 48 Addition is Left to Right Note that int total = stock + order + 1; is the same as int total = (stock + order) + 1; Note: Parentheses force the enclosed operation to be performed first (which makes no difference in this case) 49 9
Summary of the int Arithmetic Operators see next slide 50 Precedence Operator Kind Syntax Operation -5 + infix x + y add y to x - infix x - y subtract y from x Lowest priority Highest priority -4-2 -1 * infix x * y multiply x by y / infix x / y divide x by y % infix x % y remainder of x / y + prefix +x identity - prefix -x negate x ++ prefix ++x x = x + 1; result = x -- prefix --x x = x - 1; result = x ++ postfix x++ result = x; x = x + 1 -- postfix x-- result = x; x = x - 1 51 10
Division (/) Notes (1) For integer operands, the result is an integer rounded toward zero, so 5 / 4 1-5 / 4-1 For real operands, the result is a real 5.0 / 4.0 1.25-5.0 / 4.0-1.25 52 Notes (2) Remainder (%) is the remainder after division I.e., a % b yields a (a / b) * b) 5 % 3 2 5 % -3 2-5 % 3-2 Note: the sign is always the same as the divisor 53 11
Notes (3) Auto increment (++), auto decrement (--) Prefix: Increment/decrement before using in an expression Postfix: Increment/decrement after using in an expression Example: If x is 5, z = ++x leads to z being 6 If x is 5, z = x++ leads to z being 5 In both cases above, x becomes 6 54 55 12
= 5 + 1 / 5-2 * 3 % 4 56 = 5 + 1 / 5-2 * 3 % 4 57 13
= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 58 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 59 14
= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 60 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 61 15
= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 62 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 63 16
= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 = 5-2 64 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 = 5-2 = 3 65 17
Note For the full scoop on Java operators, consult Oracle s Java Tutorials: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html 66 Mixing Types and Casting Promotion (aka widening conversion) Converts a lower type to a higher type (e.g., from int to float) Done automatically, when needed May lead to loss of precision, but the magnitude is preserved Demotion (aka narrowing conversion) Converts a higher type to a lower type (e.g., from double to long) Not done automatically Done explicitly using a cast Promotion and demotion illustrated (next slide) 67 18
double float Demotion long int Promotion byte short char 68 Promotion Demotion Examples float x = 1.5f; double xsquared = x * x; double x = 1.5; float xsquared = (float)(x * x); The result is a float, but it is automatically promoted to a double when assigned to xsquared. The initial result is a double, but it is cast down (i.e., demoted) to a float when assigned to xsquared. 69 19
Notes The cast operation has a precedence that is higher than * but less than ++ The = operator has the lowest precedence of all operators There are shorthand operators to combine assignment with an operator: x op= y is shorthand for x = x op y E.g., x += 1 is like x = x + 1 70 Example int ivar = 15; long lvar = 2; float fvar = 7.6f - ivar / lvar; double dvar = 1L / lvar + fvar / lvar; int result = 100 * dvar; What is the value of result? Do in class: Put the statements into a Java program, fix if need be, and output the value of result. 71 20
Thank You 72 21