next up previous
Next: Lecture 6 Up: Home

Readings



More Programming Constructs

Representing integers

two's complement

} 

Type
Storage   Min Value   Max Value
byte
8  bits   -128           127
short
16 bits  -32,768         32,767
int
32 bits  -2,147,483,648  2,147,483,647
long
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)
Type
Storage Mantissa   Exponent
float
32 bits    23      8
double
64 bits    52      11

In floating point, half the represented
numbers are between -1 and 1.

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



Performing Conversions



Operators and Short Forms



Operators and Short Forms



Operator Precedence

1 L-R
dot, parameter evaluation,
postfix operators
2 R-L
prefix operators
3    
new object and casting
4 L-R
arithmetic
5 L-R
relational comparison i.e. >=
6 L-R
logical connectors i.e. ||
7 R-L
? :
8 R-L
assignment

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()





next up previous
Next: Lecture 6 Up: Home

Craig MacDonald
Wed Jun 10 15:58:17 EDT 1998