Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas...

22
Introduction The Basics Programming in Java Further Reading Introduction to Java Panagiotis Louridas [email protected] DIEPTELO Louridas Introduction to Java

Transcript of Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas...

Page 1: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Introduction to Java

Panagiotis Louridas [email protected]

DIEPTELO

Louridas Introduction to Java

Page 2: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Outline

1 IntroductionA Bit of HistoryThe Java Runtime EnvironmentJava Programs

2 The BasicsJava Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

3 Programming in JavaHello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

4 Further Reading

Louridas Introduction to Java

Page 3: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Outline

1 IntroductionA Bit of HistoryThe Java Runtime EnvironmentJava Programs

2 The BasicsJava Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

3 Programming in JavaHello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

4 Further Reading

Louridas Introduction to Java

Page 4: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Outline

1 IntroductionA Bit of HistoryThe Java Runtime EnvironmentJava Programs

2 The BasicsJava Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

3 Programming in JavaHello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

4 Further Reading

Louridas Introduction to Java

Page 5: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Outline

1 IntroductionA Bit of HistoryThe Java Runtime EnvironmentJava Programs

2 The BasicsJava Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

3 Programming in JavaHello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

4 Further Reading

Louridas Introduction to Java

Page 6: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

A Bit of HistoryThe Java Runtime EnvironmentJava Programs

A Bit of History

Bill Joy, 1990: Sun Aspen Smallworks

James Gosling, 1992: FirstPerson. Work on “C++ minusminus”

Demise of Apple Newton: efforts shift to interactive TV (ITV)

Oak: a language for ITV

1993: Oak becomes Java

Louridas Introduction to Java

Page 7: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

A Bit of HistoryThe Java Runtime EnvironmentJava Programs

The Java Runtime Environment

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 8: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

A Bit of HistoryThe Java Runtime EnvironmentJava Programs

Types of Java Programs

Applets

j2me

Desktop applications

Server applications

Network applications

. . .

Louridas Introduction to Java

Page 9: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

Java Compared to Other Languages

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 10: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

Java Principles

Simplify

Object Orientation

Type safety

Runtime information

Error handling

Threads

Collections

Louridas Introduction to Java

Page 11: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

What is an Object

An object is an entity containing data and code, or state andbehaviour

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

Louridas Introduction to Java

Page 12: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

A class example

c l a s s B i c y c l e {

i n t cadence = 0 ;i n t speed = 0 ;i n t g e a r = 1 ;

void changeCadence ( i n t newValue ) {cadence = newValue ;

}

void changeGear ( i n t newValue ) {g e a r = newValue ;

}

void speedUp ( i n t i n c r e m e n t ) {speed = speed + i n c r e m e n t ;

}

void a p p l y B r a k e s ( i n t decrement ) {speed = speed − decrement ;

}

void p r i n t S t a t e s ( ) {System . out . p r i n t l n ( ” cadence : ”+cadence+” speed : ”+speed+” g e a r : ”+g e a r ) ;

}}

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

Louridas Introduction to Java

Page 13: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

A Bicycle Demo

c l a s s BicycleDemo {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

// Crea te two d i f f e r e n t B i c y c l e o b j e c t sB i c y c l e b i k e 1 = new B i c y c l e ( ) ;B i c y c l e b i k e 2 = new B i c y c l e ( ) ;

// Invoke methods on tho s e o b j e c t sb i k e 1 . changeCadence ( 5 0 ) ;b i k e 1 . speedUp ( 1 0 ) ;b i k e 1 . changeGear ( 2 ) ;b i k e 1 . p r i n t S t a t e s ( ) ;

b i k e 2 . changeCadence ( 5 0 ) ;b i k e 2 . speedUp ( 1 0 ) ;b i k e 2 . changeGear ( 2 ) ;b i k e 2 . changeCadence ( 4 0 ) ;b i k e 2 . speedUp ( 1 0 ) ;b i k e 2 . changeGear ( 3 ) ;b i k e 2 . p r i n t S t a t e s ( ) ;

}}

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

Louridas Introduction to Java

Page 14: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

Inheritance

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

c l a s s MountainBike extends B i c y c l e {

// new f i e l d s and methods d e f i n i n g a mountain b i k e// would go he r e

}

Louridas Introduction to Java

Page 15: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Java Compared to Other LanguagesJava PrinciplesObject Orientation in a Nutshell

InterfacesInterfaces are like classes, but without implementation

i n t e r f a ce D r i v e a b l e {boolean s t a r t E n g i n e ( ) ;void s t o p E n g i n e ( ) ;f l o a t a c c e l e r a t e ( f l o a t acc ) ;boolean t u r n ( D i r e c t i o n d i r ) ;

}

c l a s s Automobi le implements D r i v e a b l e {. . .pub l i c boolean s t a r t E n g i n e ( ) {

i f ( notTooCold )e n g i n e R u n n i n g = true ;. . .

}

pub l i c void s t o p E n g i n e ( ) {e n g i n e R u n n i n g = f a l s e ;

}

pub l i c f l o a t a c c e l e r a t e ( f l o a t acc ) {. . .

}

pub l i c boolean t u r n ( D i r e c t i o n d i r ) {. . .

}. . .

}

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 16: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Hello World

pub l i c c l a s s H e l l o J a v a {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

System . out . p r i n t l n ( ” H e l l o , Java ! ” ) ;}

}

Louridas Introduction to Java

Page 17: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Graphical Hello World

import j a v a x . swing . ∗ ;

pub l i c c l a s s H e l l o J a v a S w i n g {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

JFrame frame = new JFrame ( ” H e l l o , Java ! ” ) ;J L a b e l l a b e l = new J L a b e l ( ” H e l l o , Java ! ” ,

J L a b e l . CENTER ) ;f rame . add ( l a b e l ) ;f rame . s e t S i z e (300 , 3 0 0 ) ;f rame . s e t V i s i b l e ( true ) ;

}}

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 18: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Graphical Hello World with InteractionThe main class

import j a v a . awt . ∗ ;import j a v a . awt . e v e n t . ∗ ;import j a v a x . swing . ∗ ;

pub l i c c l a s s H e l l o J a v a 2 {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

JFrame frame = new JFrame ( ” H e l l o J a v a 2 ” ) ;f rame . getContentPane ( )

. add (new Hel loComponent ( ” H e l l o , Java ! ” ) ) ;f rame . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE ) ;f rame . s e t S i z e (300 , 3 0 0 ) ;f rame . s e t V i s i b l e ( true ) ;

}}

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 19: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Graphical Hello World with InteractionThe HelloComponent class

c l a s s Hel loComponent extends JComponentimplements M o u s e M o t i o n L i s t e n e r {

S t r i n g theMessage ;i n t messageX = 125 , messageY = 9 5 ; // Coo rd i n a t e s o f the message

pub l i c Hel loComponent ( S t r i n g message ) {theMessage = message ;addMouseMot ionL i s tener ( t h i s ) ;

}

pub l i c void paintComponent ( G r a p h i c s g ) {g . d r a w S t r i n g ( theMessage , messageX , messageY ) ;

}

pub l i c void mouseDragged ( MouseEvent e ) {// Save the mouse c o o r d i n a t e s and pa i n t the message .messageX = e . getX ( ) ;

messageY = e . getY ( ) ;r e p a i n t ( ) ;

}

pub l i c void mouseMoved ( MouseEvent e ) { }}

Learning Java, 3rd Edition, O’Reilly, 2005

Louridas Introduction to Java

Page 20: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Sort Arguments

import j a v a . u t i l . ∗ ;

pub l i c c l a s s FindDups {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

Set<S t r i n g > s = new HashSet<S t r i n g >() ;f o r ( S t r i n g a : a r g s )

i f ( ! s . add ( a ) )System . out . p r i n t l n ( ” D u p l i c a t e d e t e c t e d : ” + a ) ;

System . out . p r i n t l n ( s . s i z e ( ) +” d i s t i n c t words : ” + s ) ;

}}

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

Louridas Introduction to Java

Page 21: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Hello WorldGraphical Hello WorldGraphical Hello World with InteractionCollections

Shuffle Arguments

import j a v a . u t i l . ∗ ;

pub l i c c l a s s S h u f f l e {pub l i c s t a t i c void main ( S t r i n g [ ] a r g s ) {

L i s t <S t r i n g > l i s t = A r r a y s . a s L i s t ( a r g s ) ;C o l l e c t i o n s . s h u f f l e ( l i s t ) ;System . out . p r i n t l n ( l i s t ) ;

}}

The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006

Louridas Introduction to Java

Page 22: Introduction to Java · The Java Tutorial, 4th Edition, Prentice Hall PTR, 2006 Louridas Introduction to Java. Introduction The Basics Programming in Java Further Reading Java Compared

IntroductionThe Basics

Programming in JavaFurther Reading

Further Reading

S. Zakhour, S. Hommel, J. Royal, I. Rabinovitch, T. Risser, M.Hoeber, The Java Tutorial: A Short Course on the Basics, 4th

edition, Prentice Hall PTR, 2006. Online version at: http://java.sun.com/docs/books/tutorial/index.html

J. Knudsen, P. Niemeyer, Learning Java, 3rd edition, O’ Reilly,2005.

B. Eckel, Thinking in Java, 4th edition, Prentice Hall PTR,2006.

Louridas Introduction to Java