APPENDIX A
Review of Java Fundamentals
This appendix briefly reviews the core Java concepts to learn more about the corresponding material.
Data Types and Operators
Lets begin by listing some of the basic, or primitive, data types available in Java:
- * int : Integers (numbers with no decimal part)
- * float : Decimal values
- * double : Decimal values, stored with twice the precision of a float
- * char : A single character (a letter, number, or symbol)
- * boolean : The value true or false
Another commonly used data type is String , which represents text: a set of characters. Technically, this is not a primitive data type, but it can be initialized in a similar way.
Java also uses the common binary arithmetic operators: addition, subtraction, multiplication, division (or quotient in the case of integers), and remainder, represented by the symbols + , - , * , / , and % , respectively. When used with two values of the same type, the result will also be of the same type. For example, the value of 5.0/2.0 is 2.5 , whereas the value of 5/2 is . The results are different because in the first example the values have type double , and in the second example the values have type int .
When performing arithmetic involving two types of values, the values will be converted, or cast, to the more complex type. For instance, 5.0/2 yields a value of 2.5 . If desired, a numeric value of one type can be manually cast to another type by prefacing it with the name of the desired type in parentheses. For example, (double)2 produces a value of 2.0 , whereas (int)2.5 produces the value . (When casting to an int , the value is always rounded down to the nearest integer value.)
Primitive variables can be declared and initialized with a single line of code, with the following syntax:
variableType variableName = initialValue;
Alternatively, these tasks can be carried out in separate statements:
variableType variableName;
variableName = initialValue;
In addition to using = to assign values to variables, Java provides assignment operators (for brevity), which modify the value of a variable by a constant amount. For example, the statement x = x + 5 can be replaced with the statement x += 5 . Each of the other arithmetic operations has a corresponding assignment operator: -= , *= , /= , and %= .
Numeric values can be compared with the conditional operators: == for equality, != for inequality, < for less than, <= for less than or equal to, > for greater than, and >= for greater than or equal to. The result of a comparison is a Boolean value true or false and can be stored in a Boolean variable if desired. Boolean values can be combined with the Boolean operators: && for and, || for or, and ! for not.
An array is an object that contains a fixed number of values of the same type. The length of the array is set when the array is created. The values in an arrays can be initialized when it is created (and the size will be inferred). For example, the following creates an array that contains five characters:
char[] letters = { 'g' , 'a' , 'm' , 'e' , 's' } ;
Alternatively, an array can be created with only the length specified, shown here for an array that will contain 10 integers (and the values can be set at a later time):
int[] values = new int[10];
The items in an array are accessed by their position, or index, which begins with the number 0. For example, given the preceding array named letters , letters[0] produces the value g , letters[1] produces the letter a , and so forth, up to letters[4] , which produces s . Note that the array has length 5, but the positions are numbered 0 through 4. (This is true in general; an array with length n will have indices numbered 0 through n 1.) Note that once an array is created, its size cannot be changed; trying to store a value into an array at a nonexistent index value will result in an error when the program is running.
Control Structures
The statements within a Java program are typically run one after the other in sequence. Control structures can change the order of execution, either by running some statements only when certain conditions are met or by repeating a given set of statements.
Conditional Statements
An if statement is used to specify that a certain set of statements should be run only when a certain condition (or combination of conditions or a Boolean expression) evaluates to true . For example, the following code will add 100 to the variable bonus only if the value of time is greater than 60; if the value of time is not greater than 60, the code contained within the braces will not be executed.
if (time > 60)
{
bonus += 100;
}
Any number of statements may be contained within the braces. However, if only one statement is contained within the braces, the braces may be omitted and the code will have the same results, as follows:
if (time > 60)
bonus += 100;
An if-else statement is used when you need to provide an alternative set of statements that will be executed when the associated condition evaluates to false . The following code builds on the previous example, adding the behavior that if the value of time is not greater than 60, then the value of bonus will be incremented by 50 instead.
if (time > 60)
{
bonus += 100;
}
else
{
bonus += 50;
}
On occasion, you may want to test a variable for equality against a set of values, and execute a different set of statements in each case. For example, consider the following code, which prints a message depending on whether the value of itemCount is equal to 0, 1, 2, or anything else.
if (itemCount == 0)
System.out.print("You have no items.");
else if (itemCount == 1)
System.out.print("You have a single item.");
else if (itemCount == 2)
System.out.print("You have two items.");
else
System.out.print("You have many items!");
A switch statement presents an alternative way to write this type of code (which is often easier to read). The following code features a switch statement that has exactly the same effect as the if-else statements presented previously. Each of the value comparisons in the if-else statements correspond to an occurrence of the case keyword within the switch code block, while the final else statement corresponds to the default keyword. After listing the set of statements to be executed for a given case, a break statement must be included (otherwise, the statements corresponding to the following cases will also be executed, regardless of whether the variable is equal to the value presented).
switch (itemCount)
{
case 0:
System.out.print("You have no items.");
break;
case 1:
System.out.print("You have a single item.");
break;
case 2:
System.out.print("You have two items.");
break;
default:
System.out.print("You have many items!");
}
Repetition Statements
The while statement is used to repeat a set of statements as long as a given condition is true. For example, the following code will continue to add 5 to the variable score , and subtract 1 from the value of stars , as long as the value of stars is greater than 0:
while (stars > 0)
{
score += 5;
stars -= 1;
}
A while statement is particularly useful when a set of statements needs to be repeated an unknown number of times. You must be careful when using a while statement, because if the associated condition always remains true, then the statements will continue to execute forever!
The for statement is used to repeat a set of statements a fixed number of times. In typical usage, a variable is set to an initial value, and as long as a condition involving the variable is true, a set of statements is executed. Afterward, the value of the variable is changed by a given amount, the condition is checked again, and so forth, until the given condition evaluates to false . The following example initially sets a variable n to 1, and as long as n is less than 10, adds 3 to points ; the value of n is increased by 1 with each iteration of the loop:
Next page