home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
syntax
Java Syntax
week 4
Our Mantras1
-
To call a function:
“object - dot - functionName - openParen - arguments”.
(Note that when calling a method from inside another method,
the object you are asking the question
is often yourself: “this”.)
-
To declare a function (its signature):
“returnType - functionName - openParen - parameterTypeAndNames”.
def'n of “mantra”
Java Syntax
Week 4's syntax includes all the syntax everything we covered in
week 3,
plus
long,
and casts.
We also loosen our requirements on method bodies (they can contain
any statements), and the fact that variable-initializion might happen
inside an if-else statement.
Expressions
-
expression ::=
literal-value | namevariable | method-call | ( expression ) |
expression+expression | expression-expression | expression*expression | expression/expression |
(numeric-type) expressionnumeric
|
expressionboolean
&&
expressionboolean
|
expressionboolean
||
expressionboolean
|
!expressionboolean
- literal-value ::=
numeric-literal |
string-literal |
boolean-literal
- string-literal ::=
"any-text-without-a-double-quote-or-newline"
- boolean-literal ::=
false | true
-
method-call ::= expressionobject . namemethod ( argument-list )
Expressions are things which you can type into BlueJ's Code Pad,
and they evaluate to a value, like “7+4”
and “7 + Math.sqrt( Math.abs(-16) )”.
Declarations
-
class-declaration ::=
class nameClass {
method-declaration…
}
-
method-declaration ::=
typereturn name ( parameter-declaration-list ) {
[statement...]
return-statement
}
-
type ::= numeric-type | boolean| String
-
numeric-type ::= int | double | long
-
parameter-declaration ::= type nameparam
Remember that the only time you mention a type in Java is when
you are making a declaration: either declaring a parameter, or a local variable,
or a function-signature.2
Statements
-
statement ::=
return-statement |
local-variable-declare-and-initialize |
local-variable-declaration |
local-variable-initialization |
if_else-statement |
if_else_if-statement |
-
return-statement ::= return expression;
-
local-variable ::=
local-variable-declare-and-initialize|
local-variable-declaration
local-variable-initialization
-
local-variable-declaration ::= type name;
-
local-variable-initialization ::=
name = expression; |
if_else-statement |
if_else_if-statement
-
local-variable-declare-and-initialize ::= type name = expression;
-
if_else-statement ::=
if (expressionboolean) {
statement...
}
else {
statement...
}
-
if_else_if-statement ::=
if_else-statement |
if (expressionboolean) {
statement...
}
else if_else_if-statement
Statements actually do something:
return a value, or assign to (initialize) a local-variable.
Misc (lists)
-
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
-
non-empty-argument-list ::= expression [, non-empty-argument-list]
-
empty-list ::=
Names
- names are a mix of letters, digits, and underscores,
and must begin with a letter.
By convention, start with a lower-case letter (except for class-names,
and named-constants), and use camelCase for compound words.
- nameNAMED-CONSTANT
are written in all-capital letters.
They correspond to values which are independent of the parameters to a function
(such as MINIMUM_WAGE or SALES_TAX
… or Math.PI, which is the named constant PI
living inside Math!)
- nameClass are names
which start with a capital letter, and also have the exact name as the class file.
(That is, the file PizzaServer.java contains
the class PizzaServer.)
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.
- The subscripts are merely hints — for example nameClass
is really just any legal name, but I hint that it's the class-name (and,
that it should start with an upper-case letter).
caveats
-
This grammar is not for Java, but for the Java I teach in class.
Not only does it simplify full Java, but it also intentionally
precludes (for a while) if-without-else, block-statements,
declaring a variable without initializing it immediately afterwards, etc..
-
The above grammar is not meant to be minimal,
but rather understandable.
For instance, if-else
could be omitted (it's a special case of
if-else-if),
and
yet I include both (and I don't factor them agressively).
See also:
Lies
1
Definition: Mantra:
-
gen., a slogan or phrase often repeated
-
A phrase repeated to assist concentration, esp. during meditation (orig. in Hinduism)0k
↩2Oh, when you do a cast (to convert one
numeric type to another), you mention a type there too. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs