Syntactic Elements
class SpecialTurtle extends Turtle {
public void drawTriangle( double sideLength ) {
final double DEGREES_IN_CIRCLE = 360.0;
double turnAngle = DEGREES_IN_CIRCLE / 3;
this.tailDown();
this.move(sideLength);
this.turnLeft(turnAngle);
this.move(sideLength);
this.turnLeft(turnAngle);
this.move(sideLength);
return;
} // end drawTriangle
} // end class SpecialTurtle
- Keywords (a.k.a. Reserved Words):
- Examples: class, public, static, void, new, extends, double, this, return, const
- Have a predefined meaning
- Can only be used in specific locations within your program
- class - used to define a class
- public - allows everone to access
- static - belonds to the class as a whole, not to an instance of the class
- void - a "return value" of nothing
- new - instantiate an instance (object) of a class
- extends - define a specialized version of a class
- double - defines a type - real number
- this - refers to the current object
- return - stop processing and return the specifed value, if one is given:
- final - used to define a constant:
- final double DEGREES_IN_CIRCL 360.0;
Identifiers
- Programmer selected symbolic names for data, operations, and classes
- Contain letters, numbers, underscore
- Start with letter or underscore
- Class names: Demo, Island, Turtle
- Start with capital letter (stylistic convention)
- Each subsequent word also is capitalized
- Method names: main, drawTriangle
- start with lower case letter
- Each subsequent word is capitalized
- Data names
- Constants - DEGREES_IN_CIRCLE
- values that cannot change
- All upper-case characters with underscores between words
- Variables - sideLength, turnAngle, tina
- Start with lower case
- Each subsequent word is capitalized
Operators
- Predefined by the language
- [] - array index operator
- = - gets (the value of), assignment operator
- . - dot operator - send the message on the right to the object on the left. Not the same as decimal point in number.
- + - addition operator
- - - subtraction operator
- * - multiplication operator
- / - division operator
- % - modulus operator - remainder from division
Literals
- A.K.A. Manifest constants
- String
- Primitives
- boolean
- Used to represent the values true and false
- Char
- Uses 16-bits to represent a unicode glyph
- Examples: 'a' 'A' '1' '.' Note single quotes
- Is a number having a symbolic interpretation
- Numeric Values
- Int
- 32 bit signed integer
- Represents numbers ~ +/- 2,147,000,000
- Double
- 64-bit signed real number
- 15 digits of accuracy
- Represents numbers ~ +/- 2.2*10^308
Delimiters
- Used to separate elements of the program
- White space:
- space, tab, newline
- Are sometimes required
- Use liberally to aid in readability
- Punctuation
- () - argument lists, parameter lists, subexpressions
- {} - defines logical block (scope), array list initializers
- ; - end of a statement
- , - separate elements in a list
- Used to provide documentation on what the code does
- Ignored by the compiler/interpreter
- // everything until end of line is comment
- /* everything between is comment */
- Example:
int a /* not a good comment */ = 23;
int b = 47; // better comment
/*
* multi-line comment */
Expressions
- Produce a value when executed
- Operators combine values
- Evaluated left to right obeying operator precedence
- Subexpression with operator of higher precedence is evaluated prior to expression with lower precedence
- Example: a+b*c
- Best to always clarify precedence with ()'s: a+(b*c)
Structure of Class Definition
class Example {
// define class data members
// (constants and variables)
// define instance data members
// (constants and variables)
// define class methods
// define constructors
// define instance methods
}