CSC152 2005S, Class 10: Numeric Values in Java
Admin:
* Questions on the homework?
* No reading for tomorrow
* Today is something of a "basics" lab (no interesting questions as you had with strings)
Overview:
* Brief overview of numbers
* Lab!
/Questions on the Homework/
* Due at 11:00 a.m. on Wednesday.
* You have permission to forge the timestamp on your messages
/Numbers in Java/
* Need to represent numbers
* Java designers:
* Support efficient computation when the programmer wants it
(fairly close to hardware in representation and computation)
* Support elegant, object-oriented computation for those who care more about style
* Lots of different representations
* Primitive types (efficient)
* Fixed number of bits for each type of number
* 8 bits, 16 bits, 32 bits, 64 bits
* One of two "kinds" of numbers: integers or reals
* 8 bit integer: byte
* 16 bit integer: short
* 32 bit integer: int
* 64 bit integer: long
* 32 bit reals: float
* 64 bit reals: double
* Object types that correspond to primitive types
Byte, Short, Integer, Long, Float, and Double
* Mostly inter-convertable
* Include some amount of overhead for "look, this is an object" (I have no idea how much that is, but assume it's at least 64 bits)
* "Big" numbers: Larger range of values (more precise for reals)
BigInteger, BigDecimal
* The storage space depends on how big the value is
* Primitive types support "infix" math
a + b, a - b, a * b, a / b
* Object types have their own methods (add, multiply, etc.)
Integer i = new Integer(10);
cannot write
BigInteger bi = new BigInteger(10);
instead write
BigInteger bi = BigInteger.valueOf(10);
To print the largest Integer
pen.print("The largest integer is ");
pen.println(Integer.MAX_VALUE);
To add 1 to the largest short
short s = Short.MAX_VALUE;
s = s + 1;
pen.println(s);