Download presentation
Presentation is loading. Please wait.
1
Computer Science A 15: 17/4
2
Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language can be tricky
3
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 intx; intintx;
4
A Java Program public class A { public static void main(String[ ] args) { System.out.println(”Hello”); } or publicclassA{publicstaticvoid main(String[ ]args){System.out.println(”Hello”);} }
5
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
6
EBNF Extended 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 “]” | “[” “]”
7
Identifiers and numbers Identifier : Letter { digit | letter | ”_” } VarName : Identifier TypeName : Name ClassName : Name Number : digit { digit } [”l” | ”L”] Char : ”’” character ”’” String: ””” {character } ””” character: ”\r” | ”\n” | ”\b” | ”\f” | ”\t” | ”\”” | ”\’” | ”\\” | ….
8
Simple expressions Primary: ”(” Expression ”)” ”this” [ Arguments ] ”super” [ Arguments ] ”new” Identifier ”[” Expression ”]” { ”[” Expression ”]” } ”new” Identifier Arguments Identifier { ”.” Identifier } [ Arguments ] Number String Char Arguments: ”(” [ Expression { ”,” Expression } ] ”)”
9
Expressions Expression: 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: ”++” | ”--”
10
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” ”:”
11
Statements Statement: 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
12
Methods consist of statements Block: ”{” { BlockStatement } ”}” BlockStatement : [ ”final” ] Type VariableDeclarator { ”,” VariableDeclarator } ”;” [Identifier ”:”] Statement VariableDeclarator: Identifier BracketsOpt [ ”=” VariableInitializer ] BracketsOpt: { ”[” ”]” } FormalParameters: ”(” [ FormalParameter { ”,” FormalParameter } ] ”)” FormalParameter: Type Identifier BracketsOpt
13
A class consists of methods ClassBody: ”{” { ClassBodyDeclaration } ”}” ClassBodyDeclaration: ”;” [ ”static” ] Block {Modifier } MemberDecl MemberDecl: [ ”void” | Type ] Identifier FormalParameters BracketsOpt [ ”throws” IdentifierList ] ( Block | ”;” ) Type Identifier BracketsOpt [ ”=” VariableInitializer ] VariableInitializer: ”{” [VariableInitializer { ”,” VariableInitializer } [”,”] ] ”}” Expression
14
A program consists of classes CompilationUnit: [ ”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”
15
Syntax tree Expression: x++ + ++x expression expression1 expression2 expression3 infixop ”+””+” expression3primaryprefixoppostfixop ”++”identifierprimary”++” identifier ”x””x” ”x””x”
16
Example class 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+")"; } }
17
Example Point 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
18
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
19
Cast an instanceof class A{.. } class B extends A{..void g().. } A a= new B(); // ok a.g(); // not ok If(a instanceof B){ B b= (B) a; // cast b.g(); // ok }
20
Access control Java 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
21
Subtyping and generics Tst1 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);} }
22
Avoid finally int 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);
23
More features Arrays and ArrayList Inheritance Methods, recursion Nested loops Libraries: util, io, awt, swing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.