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:
- javac Program.java — compiles Program.java to produce Program.class. If the program uses other classes, javac will look for the corresponding .class files and if necessary produce them from the corresponding .java files.
- java Program — executes the program in the Java virtual machine. Execution begins by calling the main method in the class Program.
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.CompilationUnit ::= ImportClause* TypeDeclaration*
[x] — Zero or one occurances of x.
x* — Zero or more occurances of x..
x | y — Either x or y.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 toForStatement ::= for ([StatementExpressioninit]; [Expressiontest]; [StatementExpressionupdate]) Statementbody ;Statementbody; while (Expressiontest) StatementbodyEquivalent to:ReturnStatement ::= return [Expression] ;StatementExpressioninit;
while (Expressiontest]) { Statementbody; StatementExpressionupdate }Evaluate Expression and return its value to the caller. Execution resumes at the call site.StatementExpression ::= ExpressionExpression ::= 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:
Utilities
- java.util.Vector — an unbounded array of Objects
- java.util.Hashtable — mapping from keys to values
Applets
- javax.swing — classes for making graphical user interfaces
- java.applet.Applet — interface between applets and their environment
Input/Output
- java.io.FileReader — reading from files
- java.io.FileWriter — writing to files
- java.io.File — representing files
Networking
- java.net.Socket — client endpoint for network communication
- java.net.ServerSocket — server socket
- java.net.URLConnection — abstraction for connecting to URLs
Java Tutorials
There are many Java tutorials on the Web:
- http://java.sun.com/docs/books/tutorial/ — The Java Tutorial
- http://developer.java.sun.com/developer/onlineTraining/ — Sun's Tutorials Page
University of Virginia Department of Computer Science CS 201J: Engineering Software |
Sponsored by the National Science Foundation |
cs201j-staff@cs.virginia.edu |