The rules for constructing identifiers are:
BIG GOTCHA: In Java, character case matters! An uppercase 'A' is a different letter than a lowercase 'a'. Java identifiers "anIdentifier" and "anidentifier" are NOT the same, each is a unique identifier.
Indentation helps in two ways:
public class xyzzy
{
} // end class xyzzy
Then you are ready to start defining fields and
methods. Each one is indented one level, whatever
you prefer, as long as you are consistent. I like
four character positions, but used two in this web
booklet to cut down on the size of examples.
When you add a method, type it in through its ending squiggly bracket before filling it in
public class xyzzy
{
public void swap_places()
{
}
} // end class xyzzy
And, as you add variable declarations and statements
to your methods, indent them one more level and complete
them too.
public class xyzzy
{
void swap_places()
{
if ( )
{
}
}
} // end class xyzzy
for ( int i=1; i < tbl.length; i++ )
{
if ( tbl[i] > item )
item = tbl[i];
two_x = item * 2;
}
It is easy to see which statements make up the
body of the for statement and what gets
executed when the boolean expression is
true in the if statement.
To see examples of indentations of Java
statements, see
Appendix F - Java Statements.
As an example, the forward command is defined with an input for the number of steps to move. When you invoke forward your instruction consists of the command name ("forward") followed by a number that is the actual amount to move.
Instance variables must always be accessed using the syntax:
reference_variable_identifier.instance_variable_identifier
In other programming languages, like C and FORTRAN, the word "call" is similarly used to mean pretty much the same thing, e.g., a FORTRAN statement would "CALL" a subroutine.
I first covered procedure invocation in Logo in Lesson 4, Defining Your Own Commands.
Logo has a repeat command that looks like:
repeat <count> [ <instruction> ... ]
the interpreter knows that repeat means to execute
the following list of instructions the specified count.
Java has two varieties, and one comes in two flavors. One is a lot like Logo's "repeat" command; it's just a little bit more flexible.
for ( <init-expr;> <boolean-expr;> <update-expr> )
<statement>;
Java's simpler kind of iteration is:
while ( <boolean-expr> )
<statement>;
The basic concept is that a boolean expression controls execution of a statement - which can be, and most of the time is, a block-statement. Just like the if statement, the boolean expression determines whether we execute the statement or not. The difference is that once the statement has completed, we go back and evaluate the boolean expression again - if it is still true, we execute the statement again. This continues until the boolean expression evaluates to false.
Back to Table of Contents