1 / 35

Today…

Today…. Numeric Representation, Cont.: Integer “overflow” in Java. How integers are real numbers are stored. Expressions, Cont.: Some pet peeves and bad expressions. Java keywords . ( if we have time ). Integer Overflow in Java.

avye-burt
Download Presentation

Today…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today… • Numeric Representation, Cont.: • Integer “overflow” in Java. • How integers are real numbers are stored. • Expressions, Cont.: • Some pet peeves and bad expressions. • Java keywords. (if we have time) CISC124 - Prof. McLeod

  2. Integer Overflow in Java • In Python (version 3) you do not have limits on the size of an integer. • But Java does have limits. • See FactorialDemo.java CISC124 - Prof. McLeod

  3. From Before: Integer Primitive Types in Java • For byte, from -128 to 127, inclusive (1 byte). • For short, from -32768 to 32767, inclusive (2 bytes). • For int, from -2147483648 to 2147483647, inclusive (4 bytes). • For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes). • A “byte” is 8 bits, where a “bit” is either 1 or 0. CISC124 - Prof. McLeod

  4. Storage of Integers • An “un-signed” 8 digit (one byte) binary number can range from 00000000 to 11111111 • 00000000 is 0 in base 10. • 11111111 is 1x20 + 1x21 + 1x22 + … + 1x27 = 255, base 10. CISC124 - Prof. McLeod

  5. Storage of Integers - Cont. • So, how can a negative binary number be stored? • One way is to use the Two’s Complement system of storage. • Make the most significant bit a negative number: • So, the lowest “signed” binary 8 digit number is now: 10000000, which is -1x27, or -128 base 10. CISC124 - Prof. McLeod

  6. Storage of Integers - Cont. • Two’s Complement System for 1 byte: CISC124 - Prof. McLeod

  7. Storage of Integers - Cont. • For example, the binary number 10010101 is 1x20 + 1x22 + 1x24 - 1x27 = 1 + 4 + 16 - 128 = -107 base 10 • Now you can see how the primitive integer type, byte, ranges from -128 to 127. CISC124 - Prof. McLeod

  8. Storage of Integers - Cont. • Suppose we wish to add 1 to the largest byte value: 01111111 +00000001 • This would be equivalent to adding 1 to 127 in base 10 - the result would normally be 128. • In base 2, using two’s compliment, the result of the addition is 10000000, which is -128 in base 10! • So integer numbers wrap around, in the case of overflow - no warning is given in Java! CISC124 - Prof. McLeod

  9. Storage of Integers - Cont. • An int is stored in 4 bytes using “two’s complement”. • An int ranges from: 10000000 00000000 00000000 00000000 to 01111111 11111111 11111111 11111111 or -2147483648 to 2147483647 in base 10 CISC124 - Prof. McLeod

  10. From Before: Real Primitive Types • Also called “Floating Point” Types: • float, double • For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 1038 to 7 significant digits. • For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits. CISC124 - Prof. McLeod

  11. Storage of Real Numbers • The system used to store real numbers in Java complies with the IEEE standard number 754. • Like an int, a floatis stored in 4 bytes or 32 bits. • These bits consist of 23 bits for the mantissa, 8 bits for the exponent, with one sign bit: 0 00000000 00000000 00000000 0000000 exponent mantissa sign bit CISC124 - Prof. McLeod

  12. Storage of Real Numbers - Cont. • The sign bit, s, can be 0 for positive and 1 for negative. • The exponent, e, is unsigned. The standard says the exponent is biased by 127 and that the values 0 and 255 are reserved. So the exponent can range from -126 to +127. E is the unbiased value and e is the biased value (E = e - 127). • If e is not a reserved value then assume that there is a 1 to the left of the decimal point, which is followed by the mantissa, f to yield the “significand”. • The mantissa is always less than 1 this way. CISC124 - Prof. McLeod

  13. Storage of Real Numbers - Cont. • So a value is stored as: value = (-1)s 1.f  2E • For example if s = 0, e = 125 and f = 10000000000000000000000 E = 125 – 127 = -2 The significand is 1.10000000000000000000000 value = 1  1.10000000000000000000000  2-2 CISC124 - Prof. McLeod

  14. Storage of Real Numbers - Cont. • Or, value = 0.011, which is 0.37510 • The maximum float would be for : s = 0 e = 254 f = 11111111111111111111111 • We can use a demo program written by Ron Mak at: http://www.apropos-logic.com/nc/FPFormats.html CISC124 - Prof. McLeod

  15. Storage of Real Numbers - Cont. • Gives: 3.4028235E38, which is the constant: Float.MAX_VALUE. • Changing s to 1 gives -3.4028235E38 • The smallest, non-zero normalized value has e = 1 and f = 00000000000000000000000, which gives 1.17549435E-38, which is Float.MIN_NORMAL. CISC124 - Prof. McLeod

  16. Storage of Real Numbers - Cont. • These are all “normalized” numbers because we are not using the reserved exponent values, 0 and 255. • I will skip the discussion of these special cases of subnormal or denormalized numbers – unless you are really interested! CISC124 - Prof. McLeod

  17. Storage of a Real Number, Cont. • double, (8 bytes) roughly ±4.9 x 10-308 to 15 significant digits. • The IEEE754 standard states how this number is constructed in memory: • These 8 bytes consist of 52 bits for the mantissa, 11 bits for the exponent, with one sign bit: 0 000000000000 0000000000000000000000000000000000000000000000000000 exponent mantissa sign bit CISC124 - Prof. McLeod

  18. Storage of a Real Number - Cont. • The sign bit, s, can be 0 for positive and 1 for negative. • The exponent, e, ranges from 00000000000 to 11111111111 (or 0 to 2047 in base 10) • The exponent, e, is unsigned. The IEEE754 standard says the exponent is biased by 1023 and that the values 0 and 2047 are reserved. So the exponent, E, can range from -1022 to +1023 in base 10. CISC124 - Prof. McLeod

  19. Storage of a Real Number - Cont. • The maximum double would be for : s = 0 E = 102310 f = 11111111111111111111111… (52 ones) or 1.11111…  21023 • Use Ron Mak’s program: • Gives: 1.7976931348623157e308, Double.MAX_VALUE. CISC124 - Prof. McLeod

  20. Storage of a Real Number - Cont. • The smallest double would be for : s = 0 E = -102210 f = 000000000000000000… (52 zeros) or 1.000000…  2-1022 • Gives: 2.2250738585072014e-308 • Double.MIN_NORMAL CISC124 - Prof. McLeod

  21. Special Values • These constants: • NaN • POSITIVE_INFINITY • NEGATIVE_INFINITY are all available from both the Float and Double wrapper classes as constant attributes. In the console they display as: NaN, Infinity, -Infinity CISC124 - Prof. McLeod

  22. When Bad Things Happen! • The IEE754 standard requires the following behaviour when calculations go wrong: CISC124 - Prof. McLeod

  23. When Bad Things Happen, Cont. • The standard also states that the language must somehow set a flag (like throwing an exception, for example), when something like this happens. • Java does not do this – floating point operations never throw exceptions! • However Wrapper classes supply methods to detect NaN and Infinity. • So, it is up to you (the programmer!) to check your calculations and prevent “bad things” from happening. CISC124 - Prof. McLeod

  24. IEEE754 Standard • See the following web sites for more info: http://grouper.ieee.org/groups/754/ • Or: http://en.wikipedia.org/wiki/IEEE_floating-point_standard CISC124 - Prof. McLeod

  25. Expressions, Cont. • Expressions are combinations of variables, literal values, operators, keywords, method calls, etc. • For example: int aNum = 4 + 3 * 7; // aNum is 25 int aNum = (4 + 3) * 7; // aNum is 49 (4 > 7) || (10 > -1) // yields true (5.5 >= 5) && (4 != 1.0) // yields true double circ = 3.14 * 2 * r; … CISC124 - Prof. McLeod

  26. Aside – Pet Peves! • Especially on a quiz: • Do not get == mixed up with = • Do not use the symbols ≥ and ≤ in code – the compiler will not recognize them! Use >= and <= instead. • Understand precedence instead of overusing (). CISC124 - Prof. McLeod

  27. Weird (& Bad!) Expressions • Three of these lines of code will not compile. Which ones and why not? intaNum; intbNum; System.out.println(aNum = 5); aNum = bNum = 10; (aNum = bNum) = 20; aNum = (bNum = 30); aNum = (aNum + 20); (aNum = aNum) + 20; System.out.println(aNum > bNum > 5); CISC124 - Prof. McLeod

  28. Bad Expressions, Cont. • See BadExpressions.java • As safe as Java is designed to be, it is still possible to write really poor style expressions that still compile... CISC124 - Prof. McLeod

  29. 50 Java Keywords                                          the ones we will use:     CISC124 - Prof. McLeod

  30. Java Keywords – Primitive Types CISC124 - Prof. McLeod

  31. Java Keywords – Loops CISC124 - Prof. McLeod

  32. Java Keywords – Conditionals CISC124 - Prof. McLeod

  33. Java Keywords – Exceptions CISC124 - Prof. McLeod

  34. Java Keywords – Class & Method Headers CISC124 - Prof. McLeod

  35. The “Other” Keywords (from Oracle) • const and gotoare “not used by current versions of the Java programming language.” • native“is used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.” • synchronized “when applied to a method or code block, guarantees that at most one thread at a time executes that code.” • transient “indicates that a field is not part of the serialized form of an object.” • volatile “specifies that the variable is modified asynchronously by concurrently running threads.” CISC124 - Prof. McLeod

More Related