The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
115 C
|
public String toString()
Counting Numbers (Integers) | ||||
---|---|---|---|---|
Type | Precision | Min Value | Max Value | Example Literal |
byte | 8 bits | -128 | 127 | 12 |
short | 2 bytes | -32,768 | 32,767 | -25328 |
int | 4 bytes | -2,147,483,648 | 2,147,483,647 | 175625 |
long | 8 bytes | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 528000L |
Measuring Numbers (Reals) | ||||
Type | Precision | Min Value | Max Value | Example Literal |
float | 4 bytes | ±1.4×10-45 | ±3.4028235×1038 | -75.23F |
double | 8 bytes | ±4.9×10-324 | ±1.7976931348623157×10308 | 324E-100 |
int
and double
.
variable = expression;
object.method(expression, ..., expression)
return
statementreturn expression;
"Skamper skamper skamper"
length
void
methodscrossSection.getArea()
unary-operator expression
- numberRemoved
expression binary-operator expression
length * width
(expression)
(3 + 4)
-
minus
+
addition
-
subtraction
*
multiplication
/
division
%
modulus
*
, /
, and %
before
+
and -
3 + 4 * 2
= 11
(3 + 4) * 2
= 14
100 / 50 / 2
= 1
100 / (50 / 2)
= 4
variable op= expression
short for
variable = variable op expression
x += y;
short for x = x + y;
++variable
or --variable
means increment (decrement) by 1, then use.x = ++y;
is equivalent to {y = y+1; x = y;}
variable++
or variable--
means use, then increment (decrement) by 1.x = y--;
is equivalent to {x = y; y = y-1;}
expression op expression
is
the precision of the more precise expression
. The
value of the other is converted (coerced) to a more precise
representation first.
bsh % print(10/6); 1 bsh % print(10%6); 4 bsh % print(10/6.0); 1.6666666666666667 bsh % int x = 10; bsh % double y = 6.0; bsh % print(x/y); 1.6666666666666667 bsh % print(x); 10 bsh % print(3 / 4 * 256); 0 bsh % print(3 * 256 / 4); 192 bsh % print(5E20F + 10E12F + 10E12F + 10E12F + 10E12F + 10E12F); 5.0E20 bsh % print(10E12F + 10E12F + 10E12F + 10E12F + 10E12F + 5E20F); 5.0000005E20
x
is more precise than
expression
in x =
expression
, the value of expression
is converted to the precision of x
before
assignment.expression
is more precise than
x
, it is an error.
Program
public static void main (String[] args) { int x = 10; double y = 6.0; System.out.println(x + " " + y); y = x; System.out.println(x + " " + y); }prints
10 6.0 10 10.0
With 7 bits, can represent 128 numbers, e.g. 0, ..., 127.
But what about negatives: -0, ..., -127?
First idea (sign + magnitude):
3 = 00000011 0 = 00000000 -3 = 10000011 -0 = 10000000Bad idea: addition is different; -0 is wasteful.
Second idea (biased):
use 00000000
for -127, 11111111
for 128.
That is, to represent n, use binary representation of 127 +
n.
3 = 10000010 0 = 01111111 -3 = 01111100 -0 = 01111111Problem: 3 + -3 = 254. But see below:
Better idea: Two's Complement
To go from n to -n: complement bits, add 1
0 = 00000000 1 = 00000001 3 = 00000011 4 = 00000100 complement 11111111 11111110 11111100 11111011 add 1 0 = 00000000 -1 = 11111111 -3 = 11111101 -4 = 11111100No -0, and it's reversible.
n + -n 00000000 00000000 00000000 00000000 4 = 00000100 -3 = 11111101 -1 = 11111111 -4 = 11111100 -------- -------- sum 00000011 = 3 sum 11111001 = -7
Retrieve decimal fractional digits by repeated multiplication by 10.
.32054 3.2054 2.054 0.54 5.4 4.So retrieve binary fractional bits by repeated multiplication by 2.
.1101 1.101 1.01 0.1 1.Even if "bits" are in decimal notation.
.25 0.5 1.So, .2510 = .012
Try 0.110:
.1 0.2 0.4 0.8 1.6 1.2 0.4 repeat...So, .110 = .0001100110011...2
Demonstration:
/projects/CSE115/Classlibs/Demos/Counter/App.java
1. ...