Java Reference
In-Depth Information
This output uses a subheight of 5 and includes both a diamond pattern and an X
pattern. You can produce this output by changing the SUB_HEIGHT constant to 5 :
public static final int SUB_HEIGHT = 5;
and rewriting the main method as follows to produce both the original X pattern and
the new diamond pattern, which you get simply by reversing the order of the calls on
the two halves:
public static void main(String[] args) {
drawLine();
drawTop();
drawBottom();
drawLine();
drawBottom();
drawTop();
drawLine();
}
Chapter Summary
Java groups data into types. There are two major cate-
gories of data types: primitive data and objects. Primitive
types include int (integers), double (real numbers),
char (individual text characters), and boolean (logical
values).
Data can be converted from one type to another by an
operation called a cast.
Variables are memory locations in which values can be
stored. A variable is declared with a name and a type. Any
data value with a compatible type can be stored in the
variable's memory and used later in the program.
Values and computations are called expressions. The sim-
plest expressions are individual values, also called literals.
Some example literals are: 42 , 3.14 , 'Q' , and false .
Expressions may contain operators, as in ( 3 + 29) - 4
* 5 . The division operation is odd in that it's split into
quotient ( / ) and remainder ( % ) operations.
Primitive data can be printed on the console using the
System.out.println method, just like text strings. A
string can be connected to another value (concatenated)
with the + operator to produce a larger string. This feature
allows you to print complex expressions including num-
bers and text on the console.
Rules of precedence determine the order in which multiple
operators are evaluated in complex expressions. Multipli-
cation and division are performed before addition and sub-
traction. Parentheses can be used to force a particular
order of evaluation.
A loop is used to execute a group of statements several
times. The for loop is one kind of loop that can be used to
apply the same statements over a range of numbers or to
 
 
Search WWH ::




Custom Search