chapter 1: introducing java. 2 introduction why java applets and server side programming very rich...

9
Chapter 1: Introducing JAVA

Upload: dennis-jackson

Post on 31-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

Chapter 1: Introducing JAVA

Page 2: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

2

IntroductionWhy JAVA

• Applets and Server Side Programming

• Very rich GUI libraries

• Portability (machine independence)

• A real Object Oriented Language (plus all the advantages of OOP)

• Support International character sets (Unicode)

Page 3: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

3

Java Environment• Java 2 platform consists of two elements:

– The Java Virtual Machine (JVM)– Java Application Programming Interface (API)

• It is an interpreted language

• It was originally very slow compared to native ones.

• That is not valid now except in computationally intensive applications

Page 4: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

4

Program Development• Editing (any text editor or IDE)

• Compiling (javac) Java Object (byte) code

• JVM interprets that code and translates it to machine language code

• Try installing JDK 5.0 (or the newest one, JDK 6.0) then use a simple editor edit, compile and run a simple program

• Applets

Page 5: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

5

OOP in JAVA• What is an object? Any thing around us• A class is a a term that describes a collection of

objects with common properties• It is basically a convenient way to group things• A class defines a set of instance variables called

data members, attributes, or fields & a number of functions operating on these attributes (methods)

• An instance of a class is an existing object of that class

• Example: a Student class

Page 6: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

6

OOP in JAVA (cont.)• Example: CowboyHat classClass CowboyHat {

String type;

String color;

int size;

boolean hatOn;

void putHatOn() {

hatOn = true;

}

void takeHatOff() {

hatOn = flase;

}

}

Page 7: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

7

Operations on Objects• Methods: putHatOn(), takeHatOff()• Performing an operation on an object amounts to

calling the method that defines that operation for the object.

• Advantages of OOP:– Intuitive modeling– easier to understand and maintain– less error-prone– Longer to design but less time to write and test

Page 8: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

8

Encapsulation• Encapsulation: Hiding implementation• Variables and method can be hidden by using the private keyword

• You can change the internal details of the class without affecting other modules

• By using a method to set or change the values of a variable you can ensure that only legal values are set

• Defining a class is equivalent to defining a new data type

Page 9: Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A

9

JAVA’s Class Library• Very very rich Library:

– Java.lang, java.io, java.util, javax.swing, java.awt, java.awt.event, etc.

– Java.lang is automatically included

• To include a library in your program you use the import statement (import java.io.*;)

• Every Java application contains at least one class that defines the main() function, the starting execution point. Its form:

(public static void main (String [] args) { })• First program