First time here? Please, start at the beginning.

Thursday, September 16

Rewind: back to syntax

I've decided to back up a bit, away from uChat, and talk more about syntax. Some of this is near universal to all programming languages, some is more Java-specific.

Statements and expressions
Put simply, a statement is any executable line of code in your program. Several languages (Java, C et. al.) require a semicolon at the end of each statement, others don't (optional in perl).

Before I delve too deeply, a couple more terms. A "token" is the compiler's equivalent of a word; it generally has no meaning by itself. Keywords, operators, and identifiers are all tokens, as are braces and other punctuation-like symbols. An "identifier" is a name, it can be the name of a variable, a method, a class, or even a package.

A statement is generally built from expressions. An expression is a token or string of tokens that evaluate to a value. Time for an example:

(Assume that x is an int variable.)
5;     // expression: integer literal (also, by itself, a compiler error)
x = 5; // statement: puts the value on the right (5) in the variable on the left
3 + 2; // expression: integer literal, evaluates to five (also a compiler error)
x = 3 + 2; // statement

A method call is also an expression: is evaluates to the return value of the method. For example, the Integer.parseInt() method accepts a String and returns an int.

No comments:

Post a Comment