Meta Language

Java Meta Language and Examples

The meta-language in this section is an augmented version of that in David Eck’s online textbook. I have collected the meta language throughout David’s book, augmented, and reformatted it into this section.


  • <identifier>s first character must be letter, $, _
  • <identifier>s subsequent characters must be letter, digit, $, _
  • <identifier>s are names for variables, methods, classes, enum
  • Lowecase and uppercase letters are different, which means Gusty and gusty are two different identifiers.
  • Any meta language with name is an <identifier>. For example, <class-name> and <type-name> are <identifier>s.


Expressions – syntactically correct combination of variables, literals, operators, and method calls that evaluate to a value with a specific type. The following meta language shows where expressions are found within Java statements

Expressions within Statements

Operator Precedence

Simple expressions are easy to determine the order of operator evaluation. Complex expressions should contain parentheses to help the reader understand the order of operator evaluation.

Description Operators
Unary ++ -- ! unary - unary + type-cast
Multiply Divide * / %
Add Subtract + -
Relational < > <= >=
Equality == !=
Boolean and &&
Boolean or ||
Conditional ?:
Assignment = += -= *= /= %=

Operators on the same line have the same precedence. When operators of the same precedence are used in an expression without parentheses, unary operators and assignment operators are evaluated right-to-left, while the remaining operators are evaluated left-to-right. You are most likely familiar with left-to-right evaluation. For example, x * y / z is evaluated as (x * y) / z. For right-to-left evaluation consider multiple assignments. i = j = k is evaluated as i = (j = k).


x = x + 1; x++; x += 1; // equivalent
x = x++ // does not change x

Conditional Expression

  • The expression evaluates to if is ```true```.
  • The expression evaluates to if is ```false```.


next = (N % 2 == 0) ? (N/2) : (3*N+1);


  • <statement-list> is zero or more ;
  • Also <statement-list> is ; ... ;
  • A ; terminates a statement.
  • <statement> is one defined in Statements ```java

The following are some example Java blocks.

	System.out.print("The answer is ");

{  // This block exchanges the values of x and y
   int temp;	// A temporary variable for use in this block.
   temp = x;	// Save a copy of the value of x in temp.
   x = y;		// Copy the value of y into x.
   y = temp;	// Copy the value of temp into y.


Class Declarations

All Java code exists with a class.

Class Declaration

  • <declaration-list> is zero or more <declaration>
  • <declaration> is <declaration-statement>
  • <declaration> is <method-defintion>
  • <declaration> is <constructor-definition>
  • A variable declared at the class level is called field.

Program Declaration

Class Declaration with main.

  • <declaration-list> is zero or more <declaration>
  • <declaration> is <declaration-statement>
  • <declaration> is <method-defintion>
  • <declaration> is <constructor-definition>
  • main is the entry point of a program.
  • The “command line” arguments are passed as parameters in the args parameter. The identifier args is used by convention. Any valid Java identifier may be used. For most of our problems, we do not pass command line arguments. BlueJ allows you to pass parameters. The following shows a program run from a command line and the values of args.

    $ java myProgram one two three
    arg[0] contains one
    arg[1] contains two
    arg[2] contains three


Statements – unit of execution terminated by a ; Java has three categories of statements.

Declaration Statements

There are several <declaration-statement>s. The meta language for each is provided in its own subsection.

Variable Declaration without Expression

  • <type-name> can be any primitive or reference type.
    • Primitive types are byte, short, int, long, float, double, char, or boolean.
    • Reference types are String, Scanner, Person, Car, and many more.
  • Variables declared without <exp> as part of your declaration are initialized as follows.
    • numeric variables to 0.
    • char variables to the null character, which is '\0' or 0.
    • boolean variables to false.
    • Reference variables are initialized to null.
  • You may declare mulitple variables by using a comma to separate them.


int numberOfPeople;
int i, j, k;
boolean isFinished;
String s;
char firstInitial, middleInitial, lastInitial;
double principal;    // Amount of money invested.
double interestRate; // Rate as a decimal, not percentage.

Variable Declaration with Expression

  • <type-name> can be any primitive type.
    • Primitive types are byte, short, int, long, float, double, char, or boolean.
  • The <exe> must evaluate to <type-name>.
  • You may declare and initialize multiple variables by using a comma to separate them.


int numberOfStudents = 23;
double x = 1.0, y = 2.0;
int i = 0, j = 1;
boolean isFinished = true;
String s = "Hello";
int number = 4;
byte bb = 120;
double myPi = Math.PI;

Object Creation Expression Statement

  • <type-name> can be any reference type.
    • Reference types are String, Scanner, Person, Car, and many more.


String s = new String("Hello");
Person p = new Person("Gusty",22), q = new Person("Emily",25);
Scanner in = new Scanner(;

enum Declaration


Season vacation;
vacation = Season.SUMMER;

Array Declaration

Declaring an array creates a reference. You need to create the actual array. This can be done at the time of declaration using the {} with a list of expressions that evaluate to the <type-name>. This can be done at the time or declaration or after the declaration using new.

int[] a = new int[100];
int x = a.length // x is 100
int[][] a = new int[5][7];
int[] b = {1,2,3,4};
b = new int[100];

Methdod Definition and Call

Method Definition

The formal-parameter-list is a sequence of variable declarations separated by commas. A formal parameter is a variable. The type of the actual parameter must match that of the formal parameter. The value of the actual parameter is copied into the formal parameter when the method is called.

A method definition has a block with a sequence of statements. The method block may contain inner blocks. The top-level meta language for a method defintion could have been written as follows to emphaise a method block.

Constructor Definition

A constructor definition is like a method defintion except

  • The <return-type> is omitted.
  • The <constructor-name> must match the <class-name>.

Method Call Statement

The actual-parameter-list is a list of expressions separated by commas. Each actual parameter expression must evaluate to the type of its corresponding formal parameter.

Assignment Statements

The most used statement in programming is an assignment statement. Everyone is familiar with assignment statements, and we have used several assignment statements in discussions prior to this. The meta language for assignment statement is as follows.

  • = is the assignment operator
    • The assignment operator is just like the other operators.
  • <variable-name> is a variable that has previously been declared.
  • <exp> evaluates to the same type as <variable-name>

The following are some example Java assignment statements where in each case the <exp> is simply a literal. You understand expressions from your previous study. We will study the details of expressions in Expressions.

x = 3.0; 
x *= 2;
x /= 3.0;       
i = 32000;
long l = 32000;  // declares l and assigns it a value
String s;
s = "Silly";
s += " Gusty";
char c;
c = 'c';

Control Flow Statements

If Statement

if ( x > y ) {
	int temp;	// A temporary variable for use in this block.
	temp = x;	// Save a copy of the value of x in temp.
	x = y;	// Copy the value of y into x.
	y = temp;	// Copy the value of temp into y.

int x = -1;				int x = -1;
if (x < 0)				if (x < 0)
	x = 1; 				x = 1;
else					if (x >= 0)
	x = 2;				x = 2;

Switch Statement

A switch statement is like a multi-way if statement.

  • <expression> must evaluate to int, short, byte, char, String, or an enum type.
  • Multiple cases can be used. The following is an example.
    case 1: case 2: case 3:
       i = 10;
  • The break is required to cause flow to the end of the switch; otherwise, flow continued with the next case.
String computerMove;
switch ( (int)(3*Math.random()) ) {
   case 0:
      computerMove = "Rock";
   case 1:
      computerMove = "Paper";
      computerMove = "Scissors"; 
System.out.println("The computer’s move is " + computerMove);  // OK!

While Loop Statement

A while loop will execute loop body zero times if the <boolean-expression> is false the first time.

Most while loops have the block format.

int number = 1;                // The number to be printed.
while(number<6){               //Keep going as long as number is <6.
	number = number + 1;     // Go on to the next number.

Do While Statement

A do while loop will execute the loop body at least one time.

For Loop Statement

// Typical counting loop
for(<variable>=<min>; <variable><=<max>; <variable>++) { 

for(N=1; N<=10; N++) 
	System.out.println( N );

Break and Continue Statements

  • break - breaks out of a loop, continuing flow with the statement following the loop.
  • break - breaks out of a switch, continuing flow with the statement following the switch.
  • continue - goes back to the beginning of a loop, skipping any statements between continue and the end of the loop.


while (true) { // looks like it will run forever! 	System.out.print("Enter a positive number: ");
	N = TextIO.getlnInt();
	if(N>0) //the input value is OK, so jump out of loop
	System.out.println("Your answer must be > 0.");
// continue here after break

Try-catch Statement


double x;
try {
   x = Double.parseDouble(str);
   System.out.println( "The number is " + x );
   catch ( NumberFormatException e ) {
   System.out.println( "Not a legal number." );
   x = Double.NaN;

Empty Statement

if ( done )
   System.out.println(Not done yet.)