CS 201J 
Engineering Software
cs201j-staff@cs.virginia.edu
Schedule - Problem Sets - Exams - Lectures - Links

CS201J Java Language Guide

For many of the programming assignments in CS201J, you will use the Java programming language. We expect you to learn what you need to program in Java mostly on your own. This document summarizes the subset of Java language you are likely to use in the first few problem sets.

Operation

Java is the name given to both a programming language (the Java programming langauge), a byte code program representation (Java byte codes) and a platform (the Java virtual machine).

Source code is written in the Java programming language. Source code files are distinguished with a filename that ends in .java. The Java compiler (javac) translates a program in the Java programming language into a Java byte code representation. Byte code files have a filename that ends in .class. The Java virtual machine executes those byte codes.

The steps to execute a Java program are:

Core Syntax

This describes a simple subset of the Java language that should be sufficient for the first CS201J problem set. A complete Java grammar and language specification is available: The Java Language Specification, James Gosling, Bill Joy, Guy Steele and Gilad Brancha.

We describe the syntax using extended Backus-Naur Form.

Non-Terminal ::= Replacement — We can replace occurances of Non-Terminal with Replacement.
[x] — Zero or one occurances of x.
x* — Zero or more occurances of x..
x | y — Either x or y.
CompilationUnit ::= ImportClause* TypeDeclaration*
A file consists of zero of more type declarations. The file Program.java normally contains a declaration for the class Program.
ImportClause ::= import FullIdentifier [ . *] ;
Imports types into the namespace. The clause import cs201j.* makes it so all classes in the cs201 package are visible, so we can refer to the class cs201j.Check using just Check.
TypeDeclaration ::= ClassDeclaration
ClassDeclaration ::= class Identifier [ extends ClassName ] ClassBody
ClassBody ::= { MemberDeclaration* }
MemberDeclaration ::= Modifiers (VariableDeclaration | ConstructorDeclaration | MethodDeclaration)
A class groups variables, constructors and methods.

ConstructorDeclaration ::= Identifier FormalParameters Block
MethodDeclaration ::= TypeName Identifier FormalParameters Block
VariableDeclaration ::= TypeName Identifier [Initializer] ;
Initializer ::= = Expression

A constructor creates a new object of the class type. A method returns a value of type TypeName. Section 2.3.2 describes how method calls are evaluated.
Block ::= { BlockStatement* }
BlockStatement ::= VariableDeclaration | Statement

Statement ::= Block | IfStatement | ForStatement | WhileStatement | ReturnStatement | ExpressionStatement

IfStatement ::= if ( Expressiontest ) Statementtrue [ else Statementfalse ] ;

Evaluate Expressiontest. If it evaluates to a non-false value, the if statement evaluates to Statementtrue; otherwise, it evaluates to Statementfalse.
WhileStatement ::= while (Expressiontest) Statementbody ;
Evaluate Expressiontest. If it evaluates to a false value, there is nothing to evaluate. Otherwise, the while statement evaluates to
Statementbody; while (Expressiontest) Statementbody
ForStatement ::= for ([StatementExpressioninit]; [Expressiontest]; [StatementExpressionupdate]) Statementbody ;
Equivalent to:
StatementExpressioninit;
while (Expressiontest]) { Statementbody; StatementExpressionupdate }
ReturnStatement ::= return [Expression] ;
Evaluate Expression and return its value to the caller. Execution resumes at the call site.
StatementExpression ::= Expression

Expression ::= PlainExpression [ = PlainExpression ]
PlainExpression ::= PrimaryExpression [ InfixOperator PrimaryExpression ]
InfixOperator ::= || | && | == | != | < | > | <= | >= | + | - | * | / | %

PrimaryExpression ::= ( Expression ) | Literal | Identifier (. Identifier)* [ Arguments ]
Arguments ::= ( [Expression (, Expression)*] )

Identifier ::= a sequence of letters and digits, starting with a letter

The Java API

The Java API is a collection of useful Java classes. Documentation for the Java API is available at http://java.sun.com/j2se/1.4/docs/api/overview-summary.html.

Some classes you will find useful include:

Java Tutorials

There are many Java tutorials on the Web:

CS201J University of Virginia
Department of Computer Science
CS 201J: Engineering Software
Sponsored by the
National Science Foundation
cs201j-staff@cs.virginia.edu