23
Computer Science A 15: 17/4

Computer Science A 15: 17/4. Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Computer Science A 15: 17/4

Today• Overview of java

• How to decribe programming languages

• Main message:

• The core of java is small

• Even a small language can be tricky

Lexical rules• Lexical analysis: split the text into words

• Rule: split at the longest legal sequence

• Spaces are almost always ignored

• You can have spaces but not line breaks in text strings

• xጣ++ጣ+ጣ++ጣx or x++ጣ++ጣ+x

• intጣx; intጣintx;

A Java Programpublic class A{

public static void main(String[ ] args){

System.out.println(”Hello”);}

}orpublicጣclassጣAጣ{ጣpublicጣstaticጣvoidጣmainጣ(ጣStringጣ[ ]ጣargsጣ)ጣ{ጣSystemጣ.ጣoutጣ

.ጣprintlnጣ(ጣ”Hello”ጣ)ጣ;ጣ} ጣ}

Java Language Specification

A detailed definition of the the programming language. It describes all language constructions in java, syntax, semantics og pragmatics

http://java.sun.com/docs/books/jls/index.html

In the following I present an extract of approximately 2/3 of Java – the parts you are likely to use. I present the syntax in EBNF

EBNFExtended Backus Naur Form

Meta language used to describe programming languages

{ text } repeat text 0 or more times [ text ] include text 0 or 1 time ( text1 | text 2 ) either use text1 or text2

Use nonterminals to allow recursive descriptions:Exp is a nonterminal are describe balanced expressions

Exp : “[“ Exp “]” | “[” “]”

Identifiers and numbersIdentifier : Letter { digit | letter | ”_” }

VarName : Identifier

TypeName : Name

ClassName : Name

Number : digit { digit } [”l” | ”L”]

Char : ”’” character ”’”

String: ””” {character } ”””

character: ”\r” | ”\n” | ”\b” | ”\f” | ”\t” | ”\”” | ”\’” | ”\\” | ….

Simple expressionsPrimary: ”(” Expression ”)”

”this” [ Arguments ]

”super” [ Arguments ]

”new” Identifier ”[” Expression ”]” { ”[” Expression ”]” }

”new” Identifier Arguments

Identifier { ”.” Identifier } [ Arguments ]

Number

String

Char

Arguments: ”(” [ Expression { ”,” Expression } ] ”)”

ExpressionsExpression: Expression1 [AssignmentOperator Expression1]

AssignmentOperator: ”=” | ”+=” | ”-=” | ”*=” | ”/=” | ”&=” | ”|=” | ”^=” | ”%=” | ”<<=” | ”>>=” | ”>>>=”

Expression1: Expression2 [ ”?” Expression ”:” Expression1 ]

Expression2 : Expression3 { Infixop Expression3 }

Expression3 ”instanceof” Type

Infixop: ”||” | ”&&” | ”|” | ”^” | ”&” | ”==” | ”!=” | ”<” | ”>” | ”<=” | ”>=” | ”<<” | ”>>” | ”>>>” | ”+” | ”-” | ”*” | ”/” | ”%”

Expression3: PrefixOp Expression3

”(“ Type “)” Expression3

Primary {Selector } {PostfixOp }

Selector: ”.” Identifier | ”[” Expression ”]”

PrefixOp: ”++” | ”--” | ”!” | ”~” | ”+” | ”-”

PostfixOp: ”++” | ”--”

Types, etc.Type:

Identifier { “.” Identifier } BracketsOpt

BasicType

BasicType: ”byte” | ”short” | ”char” | ”int” | ”long” | ”float” | ”double” | ”boolean”

Catches: CatchClause { CatchClause }

CatchClause: ”catch” ”(” FormalParameter ”)” Block

ForInit: Expression { ”,” Expression }

[”final”] Type VariableDeclarator { ”,” VariableDeclarator }

ForUpdate: Expression { ”,” Expression }

SwitchBlockStatementGroups: { SwitchLabel {Statement } }

SwitchLabel: ”case” Expression ”:” | ”default” ”:”

StatementsStatement: Block ”if” ”(”Expression ”)” Statement [“else” Statement ] ”for” ”(” ForInit ”;” [Expression] ”;” ForUpdate ”)”

Statement ”for” ”(” Type Identifier ”:” Expression ”)” Statement ”while” ”(”Expression ”)” Statement ”do” Statement ”while” ”(”Expression ”)” ”;” ”try” Block ( Catches | [Catches ] ”finally” Block ) ”switch” ”(”Expression

”)”{ SwitchBlockStatementGroups } ”synchronized” ”(”Expression ”)” Block ”return” [Expression ] ”;” ”throw” Expression ”;” ”break” [Identifier ] ”;” ”continue” [Identifier ] ”;” Expression ”;” Identifier ”:” Statement

Methods consist of statementsBlock: ”{” { BlockStatement } ”}” BlockStatement : [ ”final” ] Type VariableDeclarator { ”,”

VariableDeclarator } ”;” [Identifier ”:”] Statement VariableDeclarator: Identifier BracketsOpt [ ”=” VariableInitializer ] BracketsOpt: { ”[” ”]” } FormalParameters: ”(” [ FormalParameter { ”,” FormalParameter } ] ”)”FormalParameter: Type Identifier BracketsOpt

A class consists of methodsClassBody: ”{” { ClassBodyDeclaration } ”}” ClassBodyDeclaration: ”;” [ ”static” ] Block {Modifier } MemberDecl MemberDecl: [ ”void” | Type ] Identifier FormalParameters

BracketsOpt [ ”throws” IdentifierList ] ( Block | ”;” ) Type Identifier BracketsOpt [ ”=” VariableInitializer ] VariableInitializer: ”{” [VariableInitializer { ”,” VariableInitializer }

[”,”] ] ”}” Expression

A program consists of classesCompilationUnit: [ ”package” QualifiedIdentifier ”;” ] {ImportDeclaration } {ClassDeclaration }ImportDeclaration: ”import” Identifier { ”.”

Identifier } [ ”.*” ] ”;”ClassDeclaration: {Modifier } ”class” Identifier [ ”extends” Type ] [ ”implements” TypeList ] ClassBody Modifier: ”public” | ”protected” | ”private” | ”static”

| ”abstract” | ”final” | ”native” | ”synchronized” | ”transient” | ”volatile” | ”strictfp”

Syntax treeExpression: x++ + ++x

expression

expression1

expression2

expression3 expression3infixop

”+” expression3primary prefixoppostfixop

”++”identifier primary”++”

identifier”x”

”x”

Exampleclass Point{ int x; int y; Point(int x,int y){ this.x=x; this.y=y; } public String toString(){ return "("+x+","+y+")"; }}

class Point3D extends Point{ int z; Point3D(int x,int y,int z){ super(x,y); this.z=z; } public String toString(){ return "("+x+","+y+","+z+")"; }}

ExamplePoint a=new Point(2,4);

System.out.println(a.toString());

Point3D b=new Point3D(7,9,13);

System.out.println(b.toString());

A Point3D ”is-a” Point object and may be used as a Point object

Basics:class A{

int f(){return 2;}

}

class B extends A{

int f(){return 3;}

}

..

A a1=new A(); int i=a1.f(); // ok, i=2

A a2=new B(); int i=a2.f(); // ok, i=3

B b1=new A(); // not ok

B b2=new B(); int i=b2.f(); // ok, i=3

Cast an instanceofclass A{ .. } class B extends A{ ..void g().. }

A a= new B(); // oka.g(); // not okIf(a instanceof B){ B b= (B) a; // cast b.g(); // ok}

Access controlJava has four levels of controlling access to fields, methods,

and classes: • public access

– Can be accessed by methods of all classes• private access

– Can be accessed only by the methods of their own class

• protected access – Access from subclasses

• package access – The default, when no access modifier is given – Can be accessed by all classes in the same package – Good default for classes, but extremely unfortunate for

field

Subtyping and genericsTst1 t1=new Tst2();

t1.f(1); t1.f(1.0); // Double 1.0 Double 1.0

Tst2 t2=new Tst2();

t2.f(1); t2.f(1.0); // Int 1.0 Double 1.0

class Tst1{

void f(double d){System.out.println("double "+d);}

}

class Tst2 extends Tst1{

void f(double d){System.out.println("Double "+d);}

void f(int i){System.out.println("Int "+i);}

}

Avoid finallyint i=0;try{ i=Integer.parseInt( JOptionPane.showInputDialog("input"));

}catch(NumberFormatException e){ System.out.println("bad"); i=Integer.parseInt( JOptionPane.showInputDialog("input"));

} finally{ System.out.println("typed: "+i);}System.out.println("typed: "+i);

More features• Arrays and ArrayList

• Inheritance

• Methods, recursion

• Nested loops

• Libraries: util, io, awt, swing