Readings
More Programming Constructs
two's complement
}
Storage Min Value Max Value
8 bits -128 127
16 bits -32,768 32,767
32 bits -2,147,483,648 2,147,483,647
64 bits < -9 x 10 18 > 9 x 10 18
Representing Floating Point
exponent
sign * mantissa * base
We use decimal (base 10) the computer uses
binary (base 2)
Representing Characters
Unicode is the mapping from keyboard
characters to a binary representation.
Unicode is a superset of ascii code
Unicode is an unsigned 16 bit integer
allowing representation of 65,536
characters (many languages characters)
'A' is 65 decimal or 1000001 binary
A-Z is earlier than a-z and both are adjacent
in Unicode so you can shift from upper to lower
case by adding 32 which is equivalent to
adding: (int)'A' - (int)'a'
The digits represented are adjacent.
'0' is 48 (decimal) so character digits can be made integers by subtracting
48.
Type Conversion Categories
int i = 2147483647; // largest int float f = i; // f = .2147483e10
float f = 1.e10f; // or 1.e-10f int i = (int)f; // i = ???
Performing Conversions
double d = 10.5; int i = (int)d; // 10
Operators and Short Forms
Operators and Short Forms
if (condition) x = exp1; else x = exp2;
Operator Precedence
boolean b = true;
i = 3;
boolean c;
c = b = 0 < new Double("3" + ".0").doubleValue() - i++ && b;
| | | | | | | | |
9 8 6 2 1 3 5 4 7
Selection
switch (integral_expression) {
case value1: statement1;
case value2: statement2;
case value3: statement3;
default: statementd;
}
Not the same as if-then-else-if unless you use a break after each case
Iteration statements
do statement; while (condition);
for (initialization; condition; increment) statement;
initialization is a list of comma separated
assignments or empty list
condition evaluates to boolean (true or false) or empty
increment is a list of assignments or empty
break and continue in loops
To exit from the middle of a do, for, or while loop use break
while (true) {
statements;
if (condition) break;
statements;
}
To continue at the top of the loop
while (condition) {
statements;
if (condition) continue;
statements;
}
Strings
The String class allows literal assignment
without the new operator
Methods
char charAt(int) int indexOf(char) int lastIndexOf(char) boolean endsWith(String suffix) boolean startsWith(String) boolean equals(Object) boolean equalsIgnoreCase(String) int compareTo(String) String toLowerCase() String toUpperCase() int length()
StringTokenizer
Constructors
StringTokenizer(String)
StringTokenizer(String, String delimiters)
StringTokenizer(String, String delimiters,
boolean returnDelimitors)
Methods
boolean hasMoreTokens() // returns boolean String nextToken() // default last delimiters String nextToken(String delimiters) // persistent int countTokens()See Voltaire and URL_Tokens
StringBuffer Class
StringBuffer size can change dynamically
Does the Basic String operations and more
Constructors
StringBuffer(), default 16 StringBuffer(int) StringBuffer(String)
Methods
String append(char) String insert(int, char) char charAt(int) void setLength(int) String reverse() int length()