programmieren ii - java - introduction to java · week01 introduction&elementaryconceptsofjava...

41
Programmieren II - Java Introduction to Java Alexander Fraser [email protected] (Contains material from: T. Bögel, K. Spreyer, S. Ponzetto, M. Hartung) April 23, 2014 1 / 40

Upload: others

Post on 14-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Programmieren II - JavaIntroduction to Java

Alexander Fraser

[email protected]

(Contains material from: T. Bögel, K. Spreyer, S. Ponzetto, M. Hartung)

April 23, 2014

1 / 40

Page 2: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Goals of this session

2 Details, getting a Schein2 Introduction to Java, why learn Java?2 How to write a “hello world” program in java

2 / 40

Page 3: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Details

Time and PlaceWed, 14:15–15:45Place: INF 306 / SR 19Thurs, 14:15–15:45Place: INF 328 / SR 25

Exceptions so farThree Thursday holidaysTag der Arbeit : 01.05.2014Christi Himmelfahrt : 29.05.2014Fronleichnam : 19.06.2014Possible business trip: 25.06-26.06.2014

3 / 40

Page 4: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Tutorium

TutorenThis is still being worked out, expect an update very soon.

Ziele und InhalteWiederholung und EinübungKorrektur der Übungsaufgaben

4 / 40

Page 5: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Prerequisites

Programming Experience?Ideally: ≥ 2nd SemesterProgrammieren IAlternatively: good programming experience

5 / 40

Page 6: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Kursziele

ZusammenfassungErweiterung des Wissens aus “Programmieren I” und praktischerProgrammierkenntnisseObjektorientierte Sicht auf ProblemePrinzipien von gutem SoftwaredesignSchöner und verständlicher CodeEinführung in das systematische Testen von Code

6 / 40

Page 7: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Kriterien zum erfolgreichen Besuch

Formale KriterienRegelmäßige Anwesenheit & MitarbeitErfolgreiches Bearbeiten der Übungsaufgaben (65% der Punkte)Klausur

7 / 40

Page 8: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

How this should work

Aktive Teilnahme und Diskussionen während der VorlesungFragen, bis alles verstanden istBei Problemen: Feedback, Mail, SprechstundeKommentare & Dokumentation!Schreibt euren eigenen Code!

VorlesungssitzungenVorlesungssitzungen bestehen aus:

TheoriePraktischer Anwendung in Form von Implementierung & Diskussionen

8 / 40

Page 9: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Overall concepts

Building on Prog I:Java syntaxOOP (objected oriented programming)Software design

9 / 40

Page 10: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Schedule

Time ContentWeek 01 Introduction & elementary concepts of JavaWeek 02 Java language specification, syntaxWeek 03,04 Introduction to OOP & remaining java syntaxWeek 05 OOP & exceptionsWeek 06 Input/outputWeek 07 Collections & more advanced OOPWeek 08+ To be determined

10 / 40

Page 11: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Prüfungsmodalitäten

Klausur: 24.07.2014 (to be confirmed)7-8 Übungsaufgaben

ÜbungsaufgabenBearbeitungszeit: 1 – 2 WochenEinzelarbeitGegenseitige Bewertung von ÜbungsaufgabenKeine verspätete Abgabe nach Deadline→ für Verlängerung vorher melden!

11 / 40

Page 12: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Kontaktinformationen

Büro und SprechstundeSprechstunde: Dienstag, 15-16 Uhr (bitte anmelden per E-Mail)Im Neuenheimer Feld 325Diese Woche: Raum 122 (1. Stock)

EmailEmail: [email protected]

12 / 40

Page 13: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Goals of this session

2� Details, getting a Schein2 Introduction to Java, why learn Java?2 How to write a “hello world” program in java

13 / 40

Page 14: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java in buzzwords 1

Java as a programming languageSimpleObject orientedDistributedMultithreadedDynamicArchitecture neutralPortableHigh performanceRobustSecure

1http://www.oracle.com/technetwork/java/langenv-140151.html14 / 40

Page 15: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Vom Programm zum Binärcode

ProblemProzessoren sprechen nur in Bits (Maschinensprache/Maschinencode):00110101Menschen sprechen nicht in Bits (und können auch nur schlecht inBits programmieren)

⇒ Programme werden in höheren Programmiersprachen (Java, Python)geschrieben und dann eine niedrigere Sprache (Assembler, Maschinencode,Bytecode) übersetzt

15 / 40

Page 16: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

From the Program to Machine Code I

Differences Python - JavaPython is typically viewed as interpreted(Translation and execution at once)Java is typically viewed as (bytecode-)compiled(Translation first, execution later)But, actually, python is also bytecode-compiled (.pyc files)

Compiled vs. InterpretedCompiled programs run faster, but are platform-dependentInterpreted programs tend to be platform-independentJava bytecode is theoretically (and often pratically)platform-independent

16 / 40

Page 17: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

From the Program to Machine Code II

Python vs Java - my viewQuick prototyping, data munging with regular expressions: use Python(or Perl)Robustly engineered code (even more critical with multipleprogrammers): use JavaExtreme high performance with manually done memory management(painful): use C/C++

17 / 40

Page 18: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

The ultimate advantage of compiling languages

source: http://xkcd.com/303/

18 / 40

Page 19: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java Workflow I

Java development workflow.source: http://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html

19 / 40

Page 20: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java Workflow II

WorkflowQuellcode: .java-DateienCompiler generiert .class-Dateien → BytecodeBytecode ist kein nativer, plattformabhängiger CodeBytecode: Maschinensprache der Java Virtual Machine (JVM)java führt bytecode mittels der JVM auf dem Endgerät aus

Vorteile und EigenschaftenKontrollierte virtuelle MaschineJVM für alle gängigen Plattformen verfügbarJVM bietet erweiterte Funktionalität, z.B. Garbage Collector (GC)Optimierung der JVM für bestimmte Prozessoren/ArchitekturenSkalierung!

20 / 40

Page 21: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Plattformunabhängigkeit und Objektorientierung

PlattformunabhängigkeitCompiler generiert Bytecode gegen JVMAbstraktion von zentralen Betriebssystem-SchnittstellenEin-/Ausgabe, grafische Benutzerschnittstellen etc. mit gemeinsamerAPIEntwickler: Programmierung gegen Java API

ObjektorientierungZiel: Schreiben großer, fehlerfreier AnwendungenAnnahme: Menschen denken objektorientiertModellierung von Verbindungen und Kommunikation von Objekten ineiner ProgrammierspracheJava weitgehend aber nicht vollständig objektorientiert. Ausnahme:primitive Datentypen

21 / 40

Page 22: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Why Java? I

Betriebswirtschaftliche Faktoren: weite VerbreitungHoher Stellenwert im universitären UmfeldHoher Bedarf an Java-EntwicklernVerfügbarkeit vieler Bibliotheken (auch: NLP)Einführung in viele Konzepte moderner Programmierparadigmen

Zahlen a

asource: http://www.oracle.com/us/corporate/press/193190

1.1 Milliarden Desktops mit Java930 Millionen Downloads der JRE pro Jahr100% Installationsbasis auf Blu-ray Playern500 Millionen aktivierte Android-Geräte (Stand: September 2012)

22 / 40

Page 23: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Why Java? II

GeschwindigkeitJust-In-Time (JIT)-CompilerÜbersetzung von relevanten Codeteilen während der LaufzeitGeschwindigkeit in vielen Fällen vergleichbar mit kompiliertenProgrammiersprachen

SpeicherverwaltungKeine Pointer, sondern Referenzen für Verweise auf VariablenTyp der Referenz nicht änderbarDefinierbare Sichtbarkeit von VariablenGarbage Collector: automatisches Löschen von Objekten undSpeicherfreigabe bei Bedarf

23 / 40

Page 24: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Why Java? III

SyntaxSimple explicit syntax makes Java code easy to read and understandDisadvantage: “verbosity” but the IDE (for example, Eclipse) can helpwith this

Software EngineeringJava enforces good habits in software engineeringObject-oriented view forces programmers to think ahead more (muchmore on this later!)Handling possible errors (through “Exceptions”) is requiredStatic type checking reduces errors (i.e., if you try to assign a stringto an integer, or compare a string and an integer, compilation error)Disadvantage, again: “verbosity”

24 / 40

Page 25: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Paradigmen der Programmierung

Vielzahl von Kriterien zur Unterscheidung von Programmiersprachen:prozedural vs. funktional vs. deklarativ vs. objekt-orientiertlow-level vs. high-levelkompiliert vs. interpretiert vs. bytecode-kompiliert

Python vs. JavaPython: interpretiert und prozedural (größtenteils)Java: bytecode-kompiliert und objekt-orientiert

25 / 40

Page 26: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Paradigmen der Programmierung

Vielzahl von Kriterien zur Unterscheidung von Programmiersprachen:prozedural vs. funktional vs. deklarativ vs. objekt-orientiertlow-level vs. high-levelkompiliert vs. interpretiert vs. bytecode-kompiliert

Python vs. JavaPython: interpretiert und prozedural (größtenteils)Java: bytecode-kompiliert und objekt-orientiert

25 / 40

Page 27: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java Versions & IDE I

JDK, JRE, JVMZur Entwicklung: Java Platform Standard Edition (Java SE)Java Development Kit: Compiler, JVM und Java BibliothekenDownload: http://www.oracle.com/technetwork/java/javase/downloads/index.html

Alternative: OpenJDK (Referenzimplementierung)Aktuelle Java-Version: Java 8 (seit 2013)

26 / 40

Page 28: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java Versions & IDE II

Interesting Java VersionsJava 1.3: used in Java ME (e.g., games on old cell phones)Java 1.5 (also called Java 5): heavily used, examples in most of ourbooks. Beginning of “modern Java” in my view.Java 1.7 (also called Java 7): added better file manipulation (but oldways still work!)Java 1.8 (also called Java 8): adds some features that are not yetwidely usedVERSION FOR THIS COURSE: Java 7 (but Java 8 is also fine)

27 / 40

Page 29: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Goals of this session

2� Details, getting a Schein2� Introduction to Java, why learn Java?2 How to write a “hello world” program in java

28 / 40

Page 30: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Writing our first Java program

PrerequisitesJDK (JRE is not enough!)Development Environment:

Either: command line + text editor (e.g. vim)Or: IDE – integrated development environment (eclipse, netbeans, . . .)

Recommendation for this course: eclipseThis session: command line + editor

29 / 40

Page 31: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Hello World!

// file name == class namepublic class HelloWorld {

// a method (function) called "main"// parameter input: array of stringspublic static void main( String [] args ) {System.out.println( "Hello world!" );

}}

code/HelloWorld.java

1 Save2 Compile: javac HelloWorld.java → HelloWorld.class

3 Run: java HelloWorld

30 / 40

Page 32: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Anatomy of a class I

31 / 40

Page 33: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Anatomy of a class II

Layered structure (simplified)source file (.java): one class definitionclass definition: one or more methodsmethod: one or more statements

32 / 40

Page 34: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Hello World explained

33 / 40

Page 35: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Comments? Comments!

Comments in one lineComment in one line: //

// this is a comment

Comments spanning multiple linesComments spanning multiple lines: /* */

/* this is a commentthis is still a comment */

Javadoc commentsJavadoc comments: /** */

/** javadoc commentcomment continued */

34 / 40

Page 36: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Experiments

Testing the compilerWhat happens if the class name differs from the file name?Do we need a main method?Do we need to specify void?Is it possible to print numbers instead of strings?. . .

35 / 40

Page 37: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Goals of this session

2� Details, getting a Schein2� Introduction to Java, why learn Java?2� How to write a “hello world” program in java

36 / 40

Page 38: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Preparation for the next session

Install the JDK, Java version 7Adapt your environment variables if necessaryCompile and run our HelloWorld classExperiment with the code: make errors and check how the compilerbehavesRecommendation: install eclipse(http://www.eclipse.org/downloads/)(optional) bring your notebook to the lecture

37 / 40

Page 39: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

General Java Literature

The official Java Tutorialhttp://docs.oracle.com/javase/tutorial/getStarted/index.html

Eckel, B.Thinking in Java.Prentice Hall, 2006.Sierra, K. & Bates, B.Head First Java.O’Reilly Media, 2005.

Ullenboom, Ch.Java ist auch eine Insel.Galileo Computing, 2012.NOTE: see the HTML index!

38 / 40

Page 40: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Java Style

Java Style GuidesJava Ranch is widely recommended:http://www.javaranch.com/style.jsp (click on the categories!)The Google Java style guide is also interesting:http://google-styleguide.googlecode.com/svn/trunk/javaguide.html

39 / 40

Page 41: Programmieren II - Java - Introduction to Java · Week01 Introduction&elementaryconceptsofJava Week02 Javalanguagespecification,syntax Week03,04 IntroductiontoOOP&remainingjavasyntax

Special Topics

Design Patterns

Gamma, E. et al.Design Patterns. Elements of Reusable Object-Oriented Software.Addison-Wesley Longman, 2008.

Test-Driven Development

Beck, K.Test-driven development – by example.Addison-Wesley, 2005.

Android DevelopmentAndroid official (see “first app”):http://developer.android.com/training/index.htmlRecommended:http://www.makeuseof.com/tag/write-google-Android-application/

40 / 40