Skip to content

Instantly share code, notes, and snippets.

@rshepherd
Last active December 23, 2015 16:19
Show Gist options
  • Save rshepherd/6661495 to your computer and use it in GitHub Desktop.
Save rshepherd/6661495 to your computer and use it in GitHub Desktop.
Expressions versus statements
public class Expressions {
public static void main(String[] args) {
// Expressions evaluate to a value...
int z = 1 + 1;
// ^^^^^
// Statements do not.
System.out.println("This is a statement.");
// Assignments are statements
z = 2;
// .. but can contain expressions
z = 2 * z;
// Statements may contain expressions.. or they may not. (like the println above)
// An expressionless statement is only called for its 'side-effect'.
// (In this case, side-effect == printing)
// Expressions can be compound and parenthesized
z = (z + (z * 2))) * z;
// Statements cannot be compound (illegal)
// (x++)(y++);
// In the simple case, expressions cannot be on a line by themselves. (illegal)
// z + z + 1
// Statements are terminated by a semi-colon
z++;
// A block is a group of zero or more statements
// They can be used anywhere a single statement is allowed
{
System.out.println("This belongs to the block.");
System.out.println("So does this. " + z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment