• Keine Ergebnisse gefunden

Die JLS im ¨ Uberblick

N/A
N/A
Protected

Academic year: 2022

Aktie "Die JLS im ¨ Uberblick"

Copied!
14
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Heute

• Das JC Projekt

• 8 Doppelstunden≈ 1 Monat

• 322 Folien

• 4 ¨Ubungsbl¨atter

• ≈5500 Zeilen Compiler-Code

• 3 Sprachen: Java – OCaml – C

⇒ Wir brauchen eine Gesamtsicht

• Langweilig?

• Andere Sichtweise: Gesamte JLS statt Einzelkonzepte

• Behandelten Ausschnitt aufzeigen

• Sprachspezifikation lesen lernen

• Neue Zusammenh¨ange sehen

• Eigenes Wissen einordnen

• Trenne nochmals Konzept von technischer Realisierung

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 1 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 1 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 1

Vorgehen

• Streifzug durch die JLS

• Von Kapitel 1 – 18 linear durchgehen

• Zitate lesen und analysieren

• R¨uckbezug Konzepte

• Die JLS verwendet Konzepte von OO-Programmiersprachen

• Aufgabe: Die Konzepte wiedererkennen

• Erinnerung an den Stoff

• Geschlossenheit des Stoffes

• Welche Teile sind abgedeckt?

• Sind die ausgelassenen Abschnitte spezifisch OO?

• Wieviel Aufwand w¨are es, sie in JC einzubauen?

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 2 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 2 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 2

Die JLS im ¨ Uberblick

Kapitel der JLS

§1. Introduction

§2. Grammars

§3. Lexical Structure

§4. Types, Values, Variables

§5. Conversions and Promotions

§6. Names

§7. Packages

§8. Classes

§9. Interfaces

§10. Arrays

§11. Exceptions

§12. Execution

§13. Binary Compatibility

§14. Blocks and Statements

§15. Expressions

§16. Definite Assignment

§17. Threads and Locks

§18. Syntax

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 3 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 3 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 3

Syntax

§2. Grammars §3. Lexical Structure §18. Syntax

• Festlegung: Grammatik-Begriff & -Notation

• Lexikalische Syntax

• Die einzelnen Zeichen des Programmtextes

• Unicode-Zeichensatz

• Zeilenende

• Leerzeichen (white space)

• Aufspaltung Einheiten, die in der weiteren Grammatik nicht mehr unterteilt sind (tokens)

• Schl¨usselworte

• Bezeichner

• Literale (Boolean, Integer, Float, String, Character)

• Operatoren

• Separatoren (Klammern, Kommata, Strichpunkte, Punkte)

• Kommentare trennen Tokens, werden aber sonst ¨uberlesen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 4 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 4 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 4

(2)

Syntax

§2. Grammars §3. Lexical Structure §18. Syntax

• §18 enth¨alt eine zusammenfassende Grammatik

• Aquivalente zur im restlichen Text gegebenen¨

• Format besser geeignet f¨ur Programmierung des Parsers

• Keine Erkl¨arungen zur Bedeutung

• Weniger verst¨andlich f¨ur den Programmierer

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 5 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 5 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 5

JC: Syntax

• ModulLexerbehandelt lexikalische Syntax

• ModulParserbehandelt Grammatik der Sprache

• Ergebnis: Abstrakter Syntaxbaum (ModulAst)

• Konzept: Abstrakte Syntax

• Abstrahiere ¨uber Details des Programmtextes

• Repr¨asentationsunabh¨angig

• Vollst¨andig geklammerte Strings

• B¨aume

• OCaml algebraische Datentypen

⇒ W¨ahle geeignete Repr¨asentation f¨ur Programmierung

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 6 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 6 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 6

§4 Types, Values, and Variables

• Es gibt drei Arten von Typen

• Primitive Typen (int,float,double, boolean)

• Referenztypen (Klassen, Interfaces, Arrays)

• Typ des Literals null

• §4.2: Werte und Operationen von Primitiven Typen

• Bedeutung von Operatoren in §15 (Expressions)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 7 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 7 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 7

§4.3 Reference Types and Values

§4.3.1 An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

• Konzept: Referenzsemantik

• Variablen enthalt nicht selbst Objekte (Arrays), sondern Referenzen

• Anderungen an referenziertem Objekt sind f¨¨ ur alle Referenzen auf das Objekt sichtbar (observable)

• Zuweisung und Funktionsaufruf kopieren nur Referenz

• Konzept: Heap-Allokation:KvP-Teil

• null-Wert = “kein Objekt”

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 8 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 8 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 8

(3)

§4.3.1 Operationen auf Objekten

. . . und ihre Umsetzung in JC 1. Field access (:Struct-Zugriff)

2. Method invocation(: C-Funktionsaufruf mitthis-Parameter) 3. The cast operator(:Laufzeit-Typ¨uberpr¨ufung)

4. The string concatenation operator + [. . . ] will convert the reference to a String by invoking the toString method of the referenced object (nicht behandelt)

5. The instanceof operator(: Laufzeit-Typinformation)

6. The reference equality operators == and !=(: C-Zeigervergleich) 7. The conditional operator ? : (nicht behandelt)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 9 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 9 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 9

§4.3.2 The Class Object

The class Object is a superclass of all other classes. A variable of type Object can hold a reference to any object, whether it is an instance of a class or an array.

The members of Object are as follows: (nicht behandelt)

1. The method getClass returns the Class object that represents the class of the object.

2. The methodtoString [. . . ]

3. The methodsequalsand hashCode[. . . ]

4. The methodcloneis used to make a duplicate of an object.

5. The methods wait, notify, and notifyAll are used in concurrent programming using threads

6. The methodfinalize is run just before an object is destroyed

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 10 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 10 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 10

Konzept: Klassenhierachie

• Klassen bilden einen Baum mit der Vererbungsbeziehung als Kanten

• Die Wurzel des Baumes ist Object

• Klassen, die keine Superklasse angeben, erben implizit vonObject

• Polymorphie

• Deklarierter Typ einer Variable ist Supertyp der m¨oglichen Inhalte

• Eine Instanz einer Klasse C kann auch als Instanz aller Superklassen vonC betrachtet werden

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 11 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 11 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 11

§4.5 Variables

A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type (§4.2) or a reference type (§4.3). A variable always contains a value that is assignment compatible (§5.2) with its type.

• Variablen in Java verwandt mit denen von C / C++

• Variablen sindver¨anderbareSpeicherpl¨atze

• (vgl. OCaml / Mini, wo Variablen zun¨achst nur f¨ur Werte stehen)

• Umsetzung in JC: Lege f¨ur jede Java-Variable eine C-Variable des passenden Typs an

• Wir haben alle Arten (n¨achste Folie) behandelt, bis auf Arrays und Exception Handler

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 12 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 12 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 12

(4)

§4.5.3 Kinds of Variables

1. A class variableis a field declared using the keyword static (: globale C-Variable)

2. An instance variableis a field declared within a class declaration (: Struct-Komponente)

3. Array componentsare unnamed variables (nicht behandelt)

4. Method parameters name argument values passed to a method.

(: C-Funktionsparameter)

5. Constructor parameters name argument values passed to a constructor.

(: C-Funktionsparameter)

6. An exception-handler parameteris [when] an exception is caught (nicht behandelt)

7. Local variables are declared by local variable declaration statements (: lokale C-Variablen)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 13 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 13 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 13

Nachtrag: Kann man Interface-Felder beschreiben?

• Letzten Donnerstag: Felder in Interfaces

§9.3 Every field declaration in the body of an interface is implicitly public, static, and final.

• Frage: Kann man diese durch Zuweisung nicht einfach ¨andern?

:Sind die “Konstanten” wirklich konstant?

• Antwort: Ja, dennfinalbei Felder bedeutet “nicht modifizierbar”

§4.5.4 A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 14 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 14 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 14

§4.5.6 Types, Classes, and Interfaces

In the Java programming language, every variable and every expression has a type that can bedetermined at compile time.

Every object belongs to some particular class: the class that was mentioned in the creation expression that produced the object, the class whose Class object was used to invoke a reflective method to produce the object [. . . ] This class is called the class of the object.

[. . . ] An object is said to be an instance of its class and of all superclasses of its class.

Sometimes a variable or expression is said to have a ”run-time type”.

This refers to the class of the object referred to by the value of the variable or expression at run time [. . . ]

• Konzept: Statischer vs. Dynamischer Typ

• Laufzeit-Typinformation f¨ur instanceofund Cast

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 15 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 15 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 15

Typkorrektheit

§4.5.6 The compile time type limits the possible values that the variable can hold or the expression can produce at run time. If a run-time value is a reference that is not null, it refers to an object or array that has a class, and that class will necessarily be compatible withthe compile-time type.

• Konzept: Typen beschreiben Laufzeit-Daten

• Primitive Typen beschreiben primitive Werte

• Klassen (und Interfaces) beschreiben Aufbau von Objekten

• Felder

• Methoden

• Verbindung mit Superklassen (& Superinterfaces):

Der Typchecker berechnet f¨ur einen Ausdruck immer einen Supertyp des dynamischen Types

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 16 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 16 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 16

(5)

§5: Conversions and Promotions

Every expression written in the Java programming language has a type [. . . ] It is possible, however, to write an expression in acontext where the type of the expression is not appropriate.

A specific conversion from type S to type T allows an expression of type S to be treated at compile time as if it had type T instead. In some cases this will require a corresponding action at run time to check the validity of the conversion or to translate the run-time value of the expression into a form appropriate for the new type T.

• Konversionen wandeln einen Wert eines Typs s in einen Wert eines erwarteten Typs tum

• Compiler produziert Code, um Repr¨asentation des Wertes zu anzupassen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 17 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 17 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 17

§5.1 Arten der Konversionen

• Identity conversions(:nichts tun)

• Widening primitive conversions(:C-Konversion)

• Narrowing primitive conversions(:C-Konversion)

• Widening reference conversions

(:Klassen: nichts, Interfaces: andere Referenz wg. Mehrfachvererbung)

• Narrowing reference conversions

(:Laufzeitpr¨ufung des Typs, chainder Superklassen)

• String conversions(nicht behandelt)

• Value set conversions (:floating point Detail)(nicht behandelt)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 18 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 18 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 18

§5 Kontexte f¨ ur Konversionen

• Assignment conversion converts the type of an expression to the type of a specified variable. [A]ssignment conversion never causes an exception.

• Method invocation conversion is applied to each argument in a method or constructor invocation. Method invocation conversion never causes an exception.

• Casting conversion converts the type of an expression to a type explicitly specified by a cast operator [. . . ] [C]ertain casts to a reference type may cause an exception at run time.

• String conversion allows any type to be converted to type String.

• Numeric promotion brings the operands of a numeric operator to a common typeso that an operation can be performed.

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 19 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 19 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 19

§5.2 Assignment Conversion

Assignment contexts allow the use of an identity conversion, a widening primitive conversion, or a widening reference conversion. In addition, a narrowing primitiveconversion may be used if all of the following conditions are satisfied:

• The expression is a constant expression of type byte, short, char or int.

• The type of the variable is byte, short, or char.

• The value of the expression (which is known at compile time,q because it is aconstant expression) is representable in the type of the variable.

• Beispiel f¨ur Kontext: Speichern eines Wertes in einer Variablen

• Bei Zuweisungen k¨onnen nur sichere Konversionen vorkommen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 20 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 20 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 20

(6)

§5.2 Assignment Conversion

Assignment of a value of compile-time reference type S (source) to a variable of compile-time reference type T (target) is checked as follows:

• If S is a class type:

• If T is a class type, then S must either be the same class as T, or S must be a subclass of T

• If T is an interface type, then S must implement interface T

• If T is an array type, then a compile-time error occurs.

• If S is an interface type:

• If T is a class type, then T must be Object

• If T is an interface type, then T must be either the same interface as S or a superinterface of S

• If T is an array type, then a compile-time error occurs.

⇒ Klassen- / Interfacevererbung, Supertypen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 21 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 21 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 21

Konzept: Konvertierbarkeit

• Zentrale Funktionconvdes Typcheckers

• Augruf in beinahe jedem Fall vonexp

• Argumente bei Methoden- und Konstruktoraufruf

• Zuweisung an Variable

• Bedingungen von if,while, . . .

⇒ Beinahe alle Typfehler werden inconvgefunden

• Konzept: Ersetzbarkeit

• Instanzen einer Klasse sind auch Instanzen aller ihrer Superklassen

• Abstraktion: Subtypen / Supertype:Relation≤auf Typen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 22 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 22 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 22

§5.2 Assignment Conversion (Arrays)

• If S is an array type SC[], that is, an array of components of type SC:

• If T is a class type, then T must be Object

• If T is an interface type, then a compile-time error occurs unless T is the typejava.io.Serializableor the typeCloneable

• Arrays sind Objekte :Konvertierbar in Object

• Arrays implementieren keine Interfaces außer den zwei genannten

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 23 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 23 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 23

§5.2 Assignment Conversion

• If S is an array type SC[], that is, an array of components of type SC:

• [. . . ]

• If T is an array type TC[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:

• TC and SC are the same primitive type.

• TC and SC are both reference types and type SC is assignable to TC, as determined by a recursive applicationof these compile-time rules for assignability.

• Knapp aufgeschriebenmitarray(s) statt S[]

s≤t

array(s)≤array(t)

• Derarray-Typ istcovariant

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 24 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 24 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 24

(7)

§6 Names

Names are used to refer to entities declared in a program. A declared entity is a package, class type, interface type, member[. . . ] of a reference type, parameter[. . . ], orlocal variable.

§6.1 A declaration introduces an entity into a program and includes an identifier that can be used in a name to refer to this entity.

§6.2 A name is used to refer to an entity declared in a program.

• Namen sind Bezeichner, die sich auf deklarierte Entit¨aten beziehen

• Konzept: Bindung von Namen

• Lexikalische (oder statische) Bindung (:Mini)

• ModulAnnotberechnet Bindungen voraus

• AAST ersetzt Namen durch ihre referenzierten Entit¨aten

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 25 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 25 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 25

§6.3 Scope of a Declaration

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name.

[Es folgen die Definitionen der Scopes verschiedener Deklarationen] The scope of a local variable declarationin a block is the rest of the block in which the declaration appears

§6.3.1 Some declarations may beshadowed in part of their scopeby another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

• Konzept: Lexikalische Bindung:Verdeckung

• Beispiel in Mini: fun (x) -> fun (x) -> x + 2

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 26 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 26 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 26

§6.6 Accessibility

• (nicht behandelt)

• public, protected,private regeln Zugriff

• Konzept: Schnittstellen

• Module / Schnittstelle / Implementierung

• Information hiding (Parnas’ Principle)

• In Java: Interaktion zwischen

• Klassen

• Packages

• Superklassen

• Innere Klassen

• Details sind sehr umfangreich

• Allgemeine KvP, nicht strikt objekt-orientiert

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 27 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 27 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 27

§7 Packages

Programs are organized as sets of packages. Each package has itsown set of namesfor types, which helps toprevent name conflicts. A top level type isaccessible outside the packagethat declares it only if the type is declared public.

• Packages bieten nur grobe Unterteilung

• Qualifizierte Namen f¨ur Zugriff auf Klassen / Interfaces anderer Packete

• importmacht diese unter einfachem Namen verf¨ugbar

• Nurpublic-Deklarationen sind in anderen Packages sichtbar

• Konzept: Namensraum

• (nicht behandelt)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 28 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 28 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 28

(8)

§8 Classes

§8 Class declarations definenew reference types and describe how they are implemented. A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class. [. . . ]

§8.1.1.1 Anabstract class is a class that is incomplete, or to be considered incomplete. nly abstract classes may have abstract methods

§8.1.1.2 A class can be declared final if its definition is complete and no subclasses are desired or required.

§8.1.3 The optionalextends clausein a class declaration specifies the direct superclass of the current class. [. . . ] If the class declaration [. . . ] has no extends clause, then the class has the class Object as its implicit direct superclass.

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 29 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 29 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 29

Sub- und Superklassen

The subclass relationship is the transitive closure of the direct subclass relationship. A class A is a subclass of class C if either of the following is true:

• A is the direct subclass of C.

• There exists a class B such that A is a subclass of B, and B is a subclass of C, applying this definition recursively.

Class C is said to be asuperclassof class A whenever A is a subclass of C.

• Konzepte:

• Vererbung

• Superklassen

• Subtypen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 30 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 30 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 30

§8.1.4 Superinterfaces

The optional implements clause in a class declaration lists the names of interfaces that are direct superinterfaces of the class being declared. An interface type I is a superinterface of class type C if any of the following is true:

• I is adirect superinterfaceof C.

• C has some direct superinterface J for which I is a superinterface

• I is a superinterface of the direct superclassof C.

• Konzepte:

• Implementierte Interfaces von Klassen

• Subtypen / Supertypen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 31 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 31 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 31

§8.2 Class Members

The members of a class type are all of the following:

• Members inherited from itsdirect superclass

• Members inherited from anydirect superinterfaces

• Members declared in thebody of the class

[. . . ] Only members of a class that are declared protected or public are inherited by subclasses [. . . ].

Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

• Vererbung von Members symmetrisch in Superklassen und Interfaces

• Vererbung begrenzt durch Zugriffsrechte (accessibility)

• Konstruktoren werden nicht vererbt

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 32 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 32 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 32

(9)

Weitere Bestandteile von Klassen

• §8.3 Field Declarations

• §8.3.1.1 static Fields

• §8.3.2 Initialization of Fields

• §8.4 Method Declarations

• §8.4.2 Method Signature

• §8.4.3.2 static Methods

• §8.4.3.4 native Methods

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 33 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 33 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 33

Uberschreiben von Methoden ¨

§8.4.6.1 An instance method m1 declared in a class C overrides another method with the same signature, m2, declared in class A if both

1. C is a subclass of A.

2. Either

• m2 is non-private and accessible from C, or

• m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.

Moreover, if m1 is not abstract, then m1 is said toimplementall declarations of abstract methodsthat it overrides.

• Konzept: Polymorphie, dynamic dispatch

• Implementierung in JC:

• Virtuelle Tabelle

• Konstruktion der virtuellen Tabelle in Modul Layout

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 34 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 34 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 34

§8.4.7 Overloading

If two methods of a class [. . . ] have the same name but different signatures, then the method name is said to beoverloaded.

When a method is invoked, the number of actual arguments and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked

• Nicht behandelt

• Vorbereitungen getroffen

• ModulTc: find_called_cls_methkann Methode ausw¨ahlen

• Ergebnis als Propertyexp_acc_methfestgehalten

• ModulCgruft die ausgew¨ahlte Methode auf (mit eindeutigem Namen)

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 35 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 35 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 35

8.5 Member Type Declarations

A member class is a class whose declaration is directly enclosed in another class or interface declaration. Similarly, a member interface is an interface whose declaration is directly enclosed in another class or interface declaration.

When an inner class refers to an instance variable that is a member of a lexically enclosing class, the variable of the corresponding lexically enclosing instance is used.

• Nicht behandelt: Verschachtelte Klassen

• Konzepte:

• Lexikalische Bindung

• Closures f¨ur Funktionen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 36 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 36 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 36

(10)

8.8 Constructor Declarations

A constructor is used in the creation of an object that is an instance of a class.

§8.8.5 The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass

§8.8.7 If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided [. . . ] The default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

• Rolle von Konstruktoren in Instantiierungssequenz

• Aufruf einessuper-Konstruktors zur Initialisierung der Superklasse

• Konzept: Generierung eines Default-Konstruktors

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 37 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 37 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 37

§9 Interfaces

An interface declaration introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation[. . . ]

§9.1.2 If an extends clause is provided, then the interface being declared [. . . ] inherits the member types, methods, and constants of each of the other named interfaces.

These other named interfaces are thedirect superinterfaces of the interface being declared. [. . . ] The superinterface relationship is the transitive closure of the direct superinterface relationship.

• Konzepte:

• Vererbung von Schnittstelle, ohne Implementierung

• Supertypen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 38 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 38 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 38

Methoden in Interfaces

§9.4 Every method declaration in the body of an interface is implicitly abstract [. . . ]. Every method declaration in the body of an interface is implicitly public.

§9.4.1 An interface inherits from its direct superinterfaces all methods of the superinterfaces that are not overridden by a declaration in the interface.

• Klassen sind abstract, wenn sie abstrakte Methoden enthalten

• Alle Interface-Methoden sind abstrakt

• Klassen erben von Interfaces deren Methoden

⇒ nicht-abstrakte Klassen m¨ussen alle aus Interfaces geerbten Methoden

¨

uberschreiben

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 39 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 39 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 39

§10 Arrays

In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

§10.4 All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be thrown.

• Arrays sind nicht spezifisch objekt-orientiert(nicht behandelt)

• Arrays sind in Java Objekte

⇒ Sie haben Referenzsemantik

• KvP: Arrays in Java haben bounds-checks

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 40 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 40 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 40

(11)

§11 Exceptions

Every exception is represented by an instance of the classThrowable or one of its subclasses;

§11.2 A compiler [. . . ] checks, at compile time, that a program contains handlers forchecked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor.

§11.3 When an exception is thrown, control is transferred from the code that caused the exception to the nearest dynamically-enclosing catch clause of a try statement that handles the exception.

• Exceptions unterbrechen normalen Programmablauf (abrupt completion)

• In Java m¨ussen Exceptions im Methoden deklariert werden

• Exceptions sind nicht spezifisch objekt-orientiert

• Implementierung: Den Funktionsauf-Stack von C manipulieren

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 41 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 41 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 41

§12 Execution

This chapter specifies activities that occur during execution of a program.

It is organized around the life cycle of a Java virtual machine and of the classes, interfaces, and objects that form a program.

• §12.1 Virtual Machine Start-Up

• §12.2 Loading of Classes and Interfaces

• §12.3 Linking of Classes and Interfaces

:§12.3.1 Verification of the Binary Representation

• §12.4 Initialization of Classes and Interfaces

• §12.5 Creation of New Class Instances

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 42 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 42 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 42

§13 Binary Compatibility

it is often impractical or impossible to [. . . ] recompile the pre- existing binaries that directly or indirectly depend on a type that is to be changed. Instead, this specification defines a set of changes that developers are permitted to make to a package or to a class or interface type while preserving [. . . ] compatibility with existing binaries.

⇒ Wie darf sich die Implementierung von Klassen und Interfaces ¨andern, so dass die Klassendateien noch wie die vorhergehende Versionen benutzt werden k¨onnen?

⇒ Sehr technische Frage

⇒ Wenig Bezug zu OO-Sprachen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 43 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 43 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 43

§14 Blocks and Statements

The sequence of execution of a program is controlled by statements, which are executed for their effect and do not have values.

§14.1 Every statement has a normal mode of execution in which certain computational steps are carried out. If all the steps are carried out [in this mode], the statement is said tocomplete normally. However, certain events may prevent a statement from completing normally [. . . ] such statements are said tocomplete abruptly.

• A break

• A continue

• A return with no value

• A return with a given value

• A throw with a given value

• Anweisungen sind ¨ublich imperativen Sprachen

• Querbezug: Variablendeklaration & -shadowing

• Querbezug: throw,try-catch:Exceptions

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 44 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 44 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 44

(12)

§15 Expressions

§15.3 If an expression denotes a variable or a value, then the expression has a type known at compile time. The rules for determining the type of an expression are explainedseparately below for each kind of expression.

The value of an expression is always assignment compatible with the type of the expression, just as the value stored in a variable is always compatible with the type of the variable.

• Ausdr¨ucke berechnen Ergebniswerte

• Der statische Typ eines Ausdrucks beschreibt den Ergebniswert

• Der statische Typ ist eventuelle nur Supertyp des dynamischen Typs

• Der Typchecker f¨ur Ausdr¨ucke besteht aus einer Fallunterscheidung nach der Art des Ausdrucks

• F¨ur jeden m¨oglichen Fall gibt es eineTypregel

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 45 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 45 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 45

§15.5 Expressions and Run-Time Checks

There are a few places in the Java programming language where the actual class of a referenced object affects program execution in a manner that cannot be deduced from the type of the expression. They are as follows:

• Method invocation

• The instanceof operator

• Casting

• Assignment to an array component of reference type

• Exception handling

• Laufzeit-Typinformation

• Hilfsfunktionenint rt_instanceof()undvoid *rt_downcast()

• Implementierung: chainvon Super-Klassen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 46 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 46 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 46

Objekt-Orientierte Ausdr¨ ucke

• §15.8.3 this

• §15.9 Class Instance Creation Expressions

• §15.11 Field Access Expressions

• §15.12 Method Invocation Expressions

• §15.16 Cast Expressions

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 47 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 47 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 47

§15.12 Method Invocation Expressions

§15.12.4 Runtime Evaluation of Method Invocation

At run time, method invocation requires five steps.

1. a target reference may be computed.

2. the argument expressions are evaluated.

3. the accessibility of the method to be invoked is checked.

4. the actual code for the method to be executed is located.

5. a new activation frame is created [. . . ] and control is transferred to the method code.

• Hauptproblem: Schritt 4

• Virtuelle Tabellen

• Interface Dispatch Tables

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 48 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 48 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 48

(13)

§15.16 Cast Expressions

At run time, the operand value is converted by casting conversion to the type specified by the cast operator.

Not all casts are permitted by the language.

• Some casts result in an error at compile time.

• Some casts can be proven always to be correct at run time.

• some casts require a test at run time.

• Hauptproblem: Downcasts zur Laufzeit

• Brauchen Laufzeit-Typinformation

• Frage: Ist dynamischer Typ Subklasse einer gegebenen Klassen?

⇒ chainvon Superklassen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 49 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 49 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 49

§16 Definite Assignment

• Java fordert, dass Variablen vor dem ersten Lesen initialisert sind

• Weil die erste Zuweisung abh¨angig vom Kontrollfluss sein kann, ist diese Bedingung nicht so leicht zu erf¨ullen

A Java compiler must carry out aspecific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.

⇒ Standard-Techniken aus dem Compilerbau

⇒ Anwendung in javac muss genau spezifiziert werden, damit sich alle Compiler vorhersehbar geich verhalten

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 50 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 50 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 50

§17 Threads and Locks

• Threads erlauben die quasi-gleichzeitig Ausf¨uhrung verschiedener Programmteile, sogenannten Threads

• Nicht jede Ablaufreihenfolge von Programmteilen sinnvoll

• Grundfrage: Wie synchronisiert man die gleichzeitig laufenden Threads?

• Java bietet programmiersprachliche Mittel zur Arbeit mit Threads

• Locks auf Objekte

• wait, notify,notifyAll f¨ur Wrten auf Lock-Freigabe

• synchronized-Methoden und Bl¨ocken holen vor Ausf¨uhrung Lock auf this-Objekt

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 51 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 51 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 51

Einmal durch die JLS

§1. Introduction

§2. Grammars

§3. Lexical Structure

§4. Types, Values, Variables

§5. Conversions and Promotions

§6. Names

§7. Packages

§8. Classes

§9. Interfaces

§10. Arrays

§11. Exceptions

§12. Execution

§13. Binary Compatibility

§14. Blocks and Statements

§15. Expressions

§16. Definite Assignment

§17. Threads and Locks

§18. Syntax

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 52 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 52 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 52

(14)

JC: Future Work

• Zugriffsschutz

• Overloading von Methoden

• KlasseClassund Reflection

• Interfaces mit interface dispatch tables

• Nested Classes

• Arrays

• Exceptions

• Threads

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 53 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 53 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 53

Evaluation: Das JC-Projekt

3 Wir verstehen die Details der JLS

• Wir haben die grundlegenden Konzepte besprochen

• Wir haben einen ziemlich vollst¨andigen ¨Uberblick

• Wir haben nur die nicht-OO Abschnitte ausgelassen

• Wir k¨onnen neue Sprachen aus konzeptueller Sicht verstehen

3 Wir haben eine Vorstellung von der Ausf¨uhrung objekt-orientierter Programme

• Wir k¨onnen die Laufzeitkosten einsch¨atzen

• Wir k¨onnen auch eher maschinennahe Sprachen wie C++ verstehen

• Wir k¨onnen neue Sprachen aus Sicht der Ausf¨uhrung verstehen 3 Wir haben eine Vorstellung von Compilierung

• Wir k¨onnen die Behandlung einzelner Sprachkonstrukte nachvollziehen

• Wir verstehen eher Einschr¨ankungen von Sprachen

• Wir k¨onnen neue Sprachen aus Sicht der ¨Ubersetzung verstehen

M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 54 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 54 M. Gasbichler, H. Gast Zusammenfassung & Ausblick JC (OOPS, 13.12.2005) Seite 54

Referenzen

ÄHNLICHE DOKUMENTE

Applied Automata Theory (WS 2014/2015) Technische Universit¨ at Kaiserslautern.. Exercise

Fachbereich Mathematik und

What

In this exercise we want to show that the model construction for FO 2 -formulae from the lecture is optimal in the following sense: in general it does not suffice to take only

Economists like use discrete-time models more than continuous-time model in economic modeling because, on the one hand, economic data are reported in terms of discrete-time such

To match the market stochasticity we introduce the new market-based price probability measure entirely determined by probabilities of random market time-series of the

Our quantitative findings suggest that migrants prefer a remittance to arrive as cash than as groceries when stakes are high ($400), but not when they are low ($200).. This result

En búsqueda del perfeccionamiento del sistema GES para los privados, es posible considerar un estudio realizado por la Superintendencia de Salud con un censo en relación a