home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
syntax-week2
Java Syntax
week 2
Our Mantras
-
To call a function:
object-dot-functionName-openParen-arguments.
-
A function's signature:
returnType-functionName-openParen-parameterTypeAndNames
Java Syntax
Week 2's syntax includes all the syntax everything we covered in
week 1,
plus
String,
multiple parameters,
syntax for
local-variable-declaration
and
local-variable-initialization,
(and, adding “namevariable” to the allowed expression)
Major definitions
-
class-declaration ::=
class nameClass {
function-declaration…
}
-
function-declaration ::=
signature {
local-variable...;
return expression;
}
signature ::=
typereturn name ( parameter-declaration-list )
-
parameter-declaration ::= type nameparam
-
local-variable ::=
local-variable-declaration
local-variable-initialization
-
local-variable-declaration ::= type name;
-
local-variable-initialization ::= name = expression;
Expressions
-
expression ::=
literal-value | namevariable | function-call | ( expression ) |
expression+expression | expression-expression | expression*expression | expression/expression
-
function-call ::= expressionobject .namefunction( argument-list )
Miscellania
-
type ::= int | double | String
-
parameter-declaration-list
::=
empty-list |
non-empty-parameter-declaration-list
-
non-empty-parameter-declaration-list
::=
parameter-declaration [, non-empty-parameter-declaration-list]
-
argument-list ::= empty-list | non-empty-argument-list
-
empty-list ::=
-
non-empty-argument-list ::= expression [, non-empty-argument-list]
How to read Backus-Naur Form (BNF):
- colored text is replaced with
a particular instance of that type.
(Subscripts are informative only, and don't technically mean anything.)
- uncolored text is typed in literally
(not replaced with something else).
- “::=” means “consists of”.
- “|” means “or”
- “[bracket-optional-part]”
can be included, or be omitted.
- “...” means zero or more
repetitions of the preceding item.
Note that this is the syntax for Java in this class so far.
See also
Lies,
for some of the ways we are lying about Java's exact syntax.
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs