Introduction to Java

25 downloads 833 Views 113KB Size Report
O'Reilly. 1st Edition. – Head First Java, Kathy Sierra, ... Java 2 Enterprise Edition ( J2EE). • Java 2 Micro Edition .... Recommended text book. ▫ What do you need ...
CSA 1019 Imperative and OO Programming

Introduction Introduction

Mr. Mr. Charlie Charlie Abela Abela Dept. Dept.of ofArtificial ArtificialIntelligence Intelligence

Course Objectives ƒƒ Familiarise with both the Familiarise with both the Imperative Imperative and and the the Object-Oriented Object-Oriented aspects aspects of of Java Java ƒƒ Build structured, reusable, Build structured, reusable, manageable manageable platform platform independent independent code. code.

Charlie Abela

CSA1019: Introduction

2

Course Structure ƒƒ ƒƒ ƒƒ

Charlie Abela

Language Language basics basics Core Core Object Object Orientated Orientated Principles Principles Advanced Advanced topics topics

CSA1019: Introduction

3

Introduction ƒƒ 3hrs 3hrsof oflectures lecturesper perweek week –– 1hr 1hrtutorial/practice tutorial/practiceevery everyweek week

ƒƒ ƒƒ

IMP: IMP:Practice PracticeMUST MUSTbe betaken takenseriously seriously Course Course slides slides and and resources resources will will be be made made available availableatat http://staff.um.edu.mt/cabe2/lectures/csa1019.html http://staff.um.edu.mt/cabe2/lectures/csa1019.html

ƒƒ 11 assignment assignment will will be be given given (25% (25% of of final final mark) mark) ƒƒ Slides Slides are are detailed detailed but but you you should should make make your yourown ownnotes notes –– –– ––

Charlie Abela

worked workedexamples examples techniques techniques design design

CSA1019: Introduction

4

Requirements ƒƒ Recommended Recommendedcourse coursetextbooks textbooks –– Introduction Introduction toto Java Java Programming, Programming, Y. Y. Daniel DanielLiang, Liang,ISBN: ISBN:9780132221580 9780132221580 –– Head Head First First Object Object Oriented Oriented Analysis Analysis and and Design, Design, Brett Brett McLaughlin, McLaughlin, Gary Gary Pollice, Pollice, David DavidWest. West.O'Reilly. O'Reilly.1st 1stEdition. Edition. –– Head HeadFirst FirstJava, Java,Kathy KathySierra, Sierra, ISBN: ISBN:9780596009205 9780596009205

ƒƒ Java JavaCompiler Compilerand andDocumentation Documentation –– Available Available atat http://java.sun.com http://java.sun.com as as Java Java 22 SDK SDK1.6 1.6(J2SE) (J2SE) –– Documentation Documentationfrom: from: http://java.sun.com/javase/6/docs/ http://java.sun.com/javase/6/docs/

ƒƒ IDE IDE –– –– –– Charlie Abela

BlueJ: BlueJ:easy-to-use easy-to-use Eclipse: Eclipse:robust, robust,used usedininindustry/research industry/research NetBeans: NetBeans:recommended recommendedby bySun Sun CSA1019: Introduction

5

Java Language Specification ƒƒ AA general-purpose, general-purpose, concurrent, concurrent, class-based, class-based, object-oriented object-orientedlanguage. language. ƒƒ Designed Designed to to be be simple simple enough enough that that many many programmers programmers can can achieve achieve fluency fluency in in the the language. language. ƒƒ The The language language isis related related to to CC and and C++ C++ but but isis organized organized rather rather differently, differently, with with aa number number of of aspects aspects of of CC and and C++ C++ omitted omitted and and aa few few ideas ideasfrom fromother otherlanguages languagesincluded included ƒƒ Language Language specification specification isis the the technical technical definition definitionof ofthe thelanguage, language,i.e. i.e. –– ––

Charlie Abela

syntax syntaxand andsemantics semantics read readmore: more:http://java.sun.com/docs/books/jls/ http://java.sun.com/docs/books/jls/

CSA1019: Introduction

6

Java API & JDK ƒƒ API (Application API (Application Interface) Interface) contains, contains, classes classes and and interfaces interfaces

Program Program predefined predefined

–– three threeeditions: editions: •• Java Java22Standard StandardEdition Edition(J2SE) (J2SE) •• Java Java22Enterprise EnterpriseEdition Edition(J2EE) (J2EE) •• Java Java22Micro MicroEdition Edition(J2ME) (J2ME)

ƒƒ JDK JDK (Java (Java Development Development Kit) Kit) –– we we will will use use J2SE J2SE 6.0 6.0 (formerly (formerly known knownas asJDK JDK1.6) 1.6) –– set set of of programs programs for for developing developing and and testing testingJava Javaprograms. programs.

Charlie Abela

CSA1019: Introduction

7

Java Compiled & Interpreted ƒƒ Java Java compilers compilers translate translate language language code code into into an an intermediary intermediary bytecode bytecode (specifically (specifically Java Java bytecode), bytecode), using using the the javac javac command. command. ƒƒ bytecode bytecode consists consists of of machine machine instructions instructions specific specific to to an an abstract abstract or or virtual virtual machine. machine. ƒƒ code code isis then then run run on on JVM JVM (Java (Java Virtual Virtual Machine), Machine), using using the the java java command, command, that that interprets interprets and and executes executes the the generic generic Java Java bytecode. bytecode.

Charlie Abela

CSA1019: Introduction

8

Java Compiled & Interpreted ƒƒ Java Java uses uses aa combination combination of of compiling compiling and and interpreting, interpreting, and and this this presents presents aa fundamental fundamentaladvantage: advantage: –– allows allows programs programs toto be be run run on on any any machine, machine, provided provided that that the the JVM JVM isis present present on on that that machine, machine,“Write “Writeonce, once,run runanywhere”. anywhere”.

Charlie Abela

CSA1019: Introduction

9

Java Virtual Machine ƒƒ The The JVM JVM isis considered considered to to be be “virtual” “virtual” because because itit isis an an “abstract “abstract computer” computer” that thatruns runscompiled compiledjava javaprograms. programs. ƒƒ ItIt isis generally generally implemented implemented in in software software on on top top of of aa "real" "real" hardware hardware platform platformand andoperating operatingsystem. system. ƒƒ All All Java Java programs programs are are compiled compiled for for the the JVM. JVM. Therefore, Therefore, the the JVM JVM must must be be implemented implemented on on aa particular particular platform platform before before compiled compiled Java Java programs programs will will run runon onthat thatplatform. platform.

Charlie Abela

CSA1019: Introduction

10

JRE: Runtime Environment

ƒƒ subset subset of of JDK, JDK, can can be be downloaded downloaded separately separately ƒƒ contains contains modules modules required required to to run run applications applications anywhere anywhere ƒƒ no no development development utilities utilities

Charlie Abela

CSA1019: Introduction

11

Java File Structure .java .java –– source source file file created created by by the the developer developer .class .class –– bytecode bytecode file file generated generated by by the the compiler compiler .jar .jar –– compressed compressed package package file file •• easily easilyportable portable •• executable executable through through command command

java java

–jar –jar

ƒƒ There There isis no no tool tool in in JDK JDK that that allows allows for for the the creation creation of of .exe .exe files, files, however however its its possible possible to to use: use: –– Webstart: Webstart: allows allows users users to to launch launch and and manage manage the the application application from from their their browser, browser,e-mail e-mailor ordesktop desktop –– third thirdparty partysoftware software •• http://www.bysoft.se/sureshot/exej/ http://www.bysoft.se/sureshot/exej/

Charlie Abela

CSA1019: Introduction

12

Good Programmers ƒƒ Programming Programming isis synonymous synonymous to to aa strategy strategy game game –– the the best best players players know know and and understand understand the the domain domain –– they theyexperiment experimentwith withdifferent differenttactics tactics –– they theypractice practicethe thegame game

ƒƒ How Howto tobecome becomeaagood goodprogrammer? programmer? –– –– –– –– –– ––

Charlie Abela

learn learnthe thelanguage languagerules ruleswell well before beforeindulging indulginginto intocoding codingunderstand understandthe the problem problemdomain domainwell well experiment experiment by by writing writing different different code code that that does doesthe thesame/similar same/similarthings things learn learneffective effectivetesting testingand anddebugging debugging document documentyour yourcode code practice..more practice..morepractice..even practice..evenmore more practice… practice…

CSA1019: Introduction

13

Essentially ƒƒ What What do do you you need need to to start start programming programming in in Java Java –– JDK JDK(J2SE (J2SEedition) edition)version version6.0 6.0 –– J2SE J2SEdocumentation documentation –– Java JavaIDE IDE –– Recommended Recommendedtext textbook book

Charlie Abela

CSA1019: Introduction

14

Simple Java Program comment

class name

//outputs //outputs Welcome Welcome to to Java Java public public class class Welcome{ Welcome{ public public static static void void main(String[] main(String[] args){ args){ System.out.println(“Welcome System.out.println(“Welcome to to Java”); Java”); }} }}

class heading ƒƒ ƒƒ

Charlie Abela

method signature

String

Save Savethe thefile fileas asWelcome.java Welcome.java Compile Compileand andexecute execute

CSA1019: Introduction

15

Sample Output ƒƒ ƒƒ ƒƒ ƒƒ

open opencmd cmdwindow window change changedirectory directory(cd) (cd)to tosource sourcefile filedirectory directory compile compileusing usingjavac javaccommand command execute executeusing usingjava javacommand command

ƒƒ Note: Note: you you need need to to make make sure sure that that the the bin bin directory directory of of the the JDK JDK isis included included in in the the system’s system’sclasspath classpath Charlie Abela

CSA1019: Introduction

16

Exercises ƒƒ Compile Compile and and execute execute Welcome.java Welcome.java ƒƒ Create Create aa new new program program out out your your name name ƒƒ Create Create aa new new program program out out multiple multiple strings strings

Charlie Abela

CSA1019: Introduction

your your own own that that prints prints that that prints prints

17