Computer Science A 15: 17/4 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Computer Science A 15: 17/4

Description:

Lexical analysis: split the text into words. Rule: split at ... ( text1 | text 2 ) either use text1 or text2. Use nonterminals to allow recursive descriptions: ... – PowerPoint PPT presentation

Number of Views:488
Avg rating:3.0/5.0
Slides: 24
Provided by: madsros
Category:

less

Transcript and Presenter's Notes

Title: Computer Science A 15: 17/4


1
Computer Science A 15 17/4

CS A
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
  • int?x int?intx

4
A Java Program
  • public class A
  • public static void main(String args)
  • System.out.println(Hello)
  • or
  • public?class?A??public?static?void?
  • 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 -
    / ltlt gtgt
    gtgtgt
  • Expression1 Expression2 ? Expression
    Expression1
  • Expression2 Expression3 Infixop Expression3
  • Expression3 instanceof Type
  • Infixop
    ! lt gt lt gt ltlt gtgt
    gtgtgt - /
  • 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 ) SwitchBlockStatement
    Groups
  • 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
expression3
infixop

expression3
primary
prefixop
postfixop

identifier
primary

x
identifier
x
16
Example
  • class Point
  • int x int y
  • Point(int x,int y)
  • this.xx this.yy
  • public String toString()
  • return "("x","y")"
  • class Point3D extends Point
  • int z
  • Point3D(int x,int y,int z)
  • super(x,y) this.zz
  • public String toString()
  • return "("x","y","z")"

17
Example
  • Point anew Point(2,4)
  • System.out.println(a.toString())
  • Point3D bnew 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 a1new A() int ia1.f() // ok, i2
  • A a2new B() int ia2.f() // ok, i3
  • B b1new A() // not ok
  • B b2new B() int ib2.f() // ok, i3

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 t1new Tst2()
  • t1.f(1) t1.f(1.0) // Double 1.0 Double
    1.0
  • Tst2 t2new 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 i0
  • try
  • iInteger.parseInt(
  • JOptionPane.showInputDialog("input"))
  • catch(NumberFormatException e)
  • System.out.println("bad")
  • iInteger.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
Write a Comment
User Comments (0)
About PowerShow.com