Introduction to Java Programming

35
Java Programming Transparency No. 1-1 Introduction to Java Programming Cheng-Chia Chen September 2001

description

Introduction to Java Programming. Cheng-Chia Chen September 2001. Course web page. Course information. Textbook: None Main References: The Java Tutorial From Sun Java 2 SDK, Standard Edition Version 1.3 Documentation ) The Java Language Specification - PowerPoint PPT Presentation

Transcript of Introduction to Java Programming

Page 1: Introduction to  Java Programming

Java Programming

Transparency No. 1-1

Introduction to Java Programming

Cheng-Chia Chen

September 2001

Page 2: Introduction to  Java Programming

Java Programming

Transparency No. 1-2

Course web page

Page 3: Introduction to  Java Programming

Java Programming

Transparency No. 1-3

Course information

Textbook: None Main References:

The Java Tutorial From Sun Java 2 SDK, Standard Edition Version 1.3 Documentation

) The Java Language Specification Java in a Nutshell 3rd edition,David Flanagan,Oreilly, 1999.

References and Documentations: Java Foundation class in a Nutshell, by David Flanagan, O

reilly, 1999. The Java Virtual Machine http://developer.java.sun.com/developer/onlineTraining/ …

Page 4: Introduction to  Java Programming

Java Programming

Transparency No. 1-4

Course information (cont’d)

Some Development Tools and IDEs: Java 2 SDK Version 1.3 (free) BeanShell (free) IBM VisualAge for java (free for entry edition) Kawa IDE (30 days trial) Forte For Java (free for CE) Borland Builder Foundation Version 5 (free) JPad (30 days trial) …

Grading: 4~5 Programming Exercises (40 %) A java Application/System Study report (15~20 %) Final Examination (30%) Class Participation (10~15%)

Page 5: Introduction to  Java Programming

Java Programming

Transparency No. 1-5

Lecture 1. Introduction

Cheng-Chia Chen

Page 6: Introduction to  Java Programming

Java Programming

Transparency No. 1-6

Contents

1. What Is Java?

2. Features of Java

3. History of Java

4. Develop first Java program

5. Develop first Java applet

Page 7: Introduction to  Java Programming

Java Programming

Transparency No. 1-7

What is Java?

Java is

a programming langugae, defined in The Java language specification (v1,v2,v3)

a virtual machine, Defined in The java virtual machine (v1, v2)

a plateform Standard edition (jdk):

Jdk v1.0~1.02: 8 packages/212 classes

jdk v1.1~1.18: 23 packages/504 classes

Java 2 (j2se): SDK v1.2~1.22, v1.30: 59 pkg/1520 classes

Enterprise edition(j2ee): V1.2, V1.2.1 (May,2000)

Micro edition (j2me): v1.0

Page 8: Introduction to  Java Programming

Java Programming

Transparency No. 1-8

What is J2EE ?

What is the Java 2 Platform, Enterprise Edition (J2EE)? a platform that enables solutions for developing, deployin

g and managing multi-tier server-centric applications. Extend J2SE to a complete, stable, secure, fast Java platfo

rm to the enterprise level. A plateform which significantly reduces the cost and com

plexity of developing multi-tier solutions, results in services that can be rapidly deployed and easily enhanced.

Page 9: Introduction to  Java Programming

Java Programming

Transparency No. 1-9

Benefits of J2EE

1. A unified platform for building, deploying and managing enterprise-class software.

2. Allow enterprise-class application the ability to run anywhere.

3. A platform with a complete range of readily available enterprise-class services.

4. A single easy-to-learn blueprint programming model for J2EE.

5. A platform that is built upon and leverages existing IT investments and guarantees that enterprise-class software will work on multiple platforms.

Page 10: Introduction to  Java Programming

Java Programming

Transparency No. 1-10

Technologies included in J2EE

Enterprise JavaBeans, JavaServer Pages, servlets, the Java Naming and Directory InterfaceTM (JNDI), the Java Transaction API (JTA), CORBA, The JDBC data access API. Java Message Queue (JMQ)

Page 11: Introduction to  Java Programming

Java Programming

Transparency No. 1-11

What is J2ME ?

1.What is the Java 2 Platform, Micro Edition (J2ME)? The JavaTM 2 Platform, Micro Edition is the edition of the

Java 2 platform targeted at consumer electronics and embedded devices.

The J2ME technology consists of a virtual machine (KVM) and a set of APIs suitable for providing tailored runtime environments for consumer and embedded electronics.

Page 12: Introduction to  Java Programming

Java Programming

Transparency No. 1-12

Features of the java language:

Java is simple object-oriented distributed interpreted robust secure architecture-neutral portable high performance multithreaded dynamic

Page 13: Introduction to  Java Programming

Java Programming

Transparency No. 1-13

Java is SImple

Intentionally created to be syntactically similar to C/C++ Eliminates traditionally troublesome features of C/C++

Pointer arithmetic Multiple inheritance Implicit type coercions Explicit memory management Preprocessor

Eliminates features of C/C++ struct typedef union enum (Programmer controlled) operator overloading

Features included as part of base language: Threads Exception handling

Page 14: Introduction to  Java Programming

Java Programming

Transparency No. 1-14

Java is Object-oriented

Systems are built from sets of classes Classes are instantiated at runtime to give objects Objects communicate via messages passing Everything is part of a class supported OO Conecpts:

Data abstraction and Encapsulation Inheritance Polymorphism Dynamic Binding e.g. variable of Object type can hold everything

Logical cluster of classes == package

Page 15: Introduction to  Java Programming

Java Programming

Transparency No. 1-15

Java is a Distributed language

Network programming support built into JDK class library: TCP sockets UDP packets IP addresses URLs RMI (Remote Method Invocation)

Security features designed into language Network programming facilities are one of the

language's best features.

Page 16: Introduction to  Java Programming

Java Programming

Transparency No. 1-16

Java is an Interpreted language

Source code is initially compiled (javac) into architecture-neutral byte-codes

Byte-codes are interpreted by the java virtual machine (JVM) (java or Netscape)

Dynamic linking/loading (at run time) (Just In Time) JIT compilers lead to a large performa

nce increase in compilation and runtime execution

Page 17: Introduction to  Java Programming

Java Programming

Transparency No. 1-17

Java is Robust

Strongly-typed language (cf Smalltalk and VisualBasic)

Compile and runtime checking No pointer arithmetic Exception handling Automatic memory management

Page 18: Introduction to  Java Programming

Java Programming

Transparency No. 1-18

Java is secure

Generally very difficult to trick Java's security mechanisms

Depends very much on the program loader (e.g., Netscape)

The SecurityManager class defines the protocol used by the program loader

Access control list

Page 19: Introduction to  Java Programming

Java Programming

Transparency No. 1-19

Java is Architecture-neutral

Byte-codes are architecture neutral

Performance suffers by using bytecodes

Page 20: Introduction to  Java Programming

Java Programming

Transparency No. 1-20

Java is portable

Primitive type sizes are explicit - not architecture dependent

Strings and characters are (16-bit) Unicode compliant

GUI libraries give a native graphic library-independent mechanism for creating quality graphical interfaces (sort of) "They gave us a library that is good for writing programs

that look equally mediocre on the different systems."(Core Java, page 9)

Page 21: Introduction to  Java Programming

Java Programming

Transparency No. 1-21

High performance

Interpreting leads to quicker development cycle Depends what you compare it to

"Slightly faster than VB" - (Core Java, page 9) JITC(Just-In-Time Compiler) help greatly in this respect Sun’s Java HotSpot is Newest high performace JIT compil

er.

Can use native code for mission-critical performance sections of code JNI: Java Native Interface Sacrifice portability.

Page 22: Introduction to  Java Programming

Java Programming

Transparency No. 1-22

Multithreaded

Based on well-known 20 year old Hoare monitor synchronization

Thread support built into language Thread synchronization primitives supplied Garbage collector runs permanently as a low priority

background thread

Page 23: Introduction to  Java Programming

Java Programming

Transparency No. 1-23

Dynamic

Class linking, layout, name resolution and object references not resolved until run-time

Runtime Type Information (RTTI) available Can check the type of objects at run-time java.reflect.* package

Class class for dynamic instantiation Can create objects of types unkown until runtime. String sexClassName = getSex(); Object p = Class.forName(sexClassName).instance(); If(p instanceof Male) {…} else if (p instanceof Female) { … } else {… }

Page 24: Introduction to  Java Programming

Java Programming

Transparency No. 1-24

An Example

/**

* The HelloJava class implements an application that

* simply displays "Hello Java!" to the standard output.

*/

class HelloJava {

public static void main(String[] args) {

// Display "Hello Java!"

System.out.println("Hello Java!");

}

}

Page 25: Introduction to  Java Programming

Java Programming

Transparency No. 1-25

History of Java

Green Project (1990) Consumer device operating software Requirements: small size, robust, portable, extremely reliable ,real-

time performance

Oak Originally used C++, then realized a new language was needed Original requirements same as for current language

Java (1993) Intended market never eventuated WWW starting to takeoff Language design "based on" many current OO languages (e.g., C++,

Eiffel, Smalltalk, Cedar/Mesa, Objective C) 1995/5/23 Sun launched Java JDK 1.0 released early 1996/1/23

Page 26: Introduction to  Java Programming

Java Programming

Transparency No. 1-26

History of Java

1997 … 2/18: Complete JDK1.1 released 3/28: JDK1.1.1, 5/28: JDK1.1.2, 7/2: JDK1.1.3,…

1998: 9/28: JDK1.1.7, 12/8: J2SE

1999: 6/? J2ME 12/7 J2EE

Page 27: Introduction to  Java Programming

Java Programming

Transparency No. 1-27

Develop your first Java Program and applet

1. Required softwares

2. Creating Your First Application a. Create a Java Source File

b. Compile the Source File

c. Run the Program

3. Creating Your First Appleta. Create a Java Source File

b. Create related HTML files

c. Compile and run the Source File

Page 28: Introduction to  Java Programming

Java Programming

Transparency No. 1-28

Required Softwares

To write your first program, you will need: 1. The JavaTM 2 Platform, Standard Edition.

2. A text editor. Ex: NotePad, Ultraedit, EditPlus2

Page 29: Introduction to  Java Programming

Java Programming

Transparency No. 1-29

2. Creating Your First Application

The first program, HelloWorldApp, will simply display : “Hello World!".

Steps: Create a Java source file. > Notepad HelloWorldApp.java Compile the source file into a bytecode file. > javac HelloWorldApp.java Run the program contained in the bytecode file. >java HelloWorldApp

Page 30: Introduction to  Java Programming

Java Programming

Transparency No. 1-30

HelloJava.java

/**

* The HelloWorldApp class implements an application that

* simply displays "Hello World!" to the standard output.

*/

class HelloWorldApp {

public static void main(String[] args) {

// Display "Hello World!"

System.out.println("Hello World!");

}

}

Page 31: Introduction to  Java Programming

Java Programming

Transparency No. 1-31

Page 32: Introduction to  Java Programming

Java Programming

Transparency No. 1-32

Java 2 SDK Installation Instructions (for WIN32)

1. Download java 2 SDK standard edition

2. Run the Java 2 SDK executable (*.exe). determine where to install j2se. (ex: c:\java\j2se\1.3) > set JAVA_HOME= c:\java\j2se\1.3

3. Update the PATH variable so that you can type ‘java’ instead of ‘c:\java\j2se\1.3\bin\java’ to invoke javatools. > path=%JAVA_HOME%\bin;%PATH%

4. Check the CLASSPATH variable Used by javatools to determine where to find your personal (nonsystem) java class fi

les Types of java bytecodes (class files):

System : javatools know where to find them.Extensions: put in %JAVA_HOME%\jre\lib\extPersonal: via LASSPATH or –cp/–classpath options

5. Start using the Java 2 SDK tools! Java, Javac, javadoc, jdb, javap,…

Page 33: Introduction to  Java Programming

Java Programming

Transparency No. 1-33

3. Creating Your First Applet

a. Create a Java Source File: HelloWorld.javaimport java.applet.*;

import java.awt.*; /** * The HelloWorld class implements an applet that * simply displays "Hello World!". */ public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } }

Page 34: Introduction to  Java Programming

Java Programming

Transparency No. 1-34

2. Create an HTML file to cntain the applet.

<HTML>

<HEAD>

<TITLE>A Simple Program</TITLE>

</HEAD>

<BODY>

Here is the output of my program:

<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>

The applet does not work!</APPLET>

</BODY>

</HTML> Save this code to a file called Hello.html.

Page 35: Introduction to  Java Programming

Java Programming

Transparency No. 1-35

3. Compile and run the program

Compile the Source File. Javac HelloWorld.java

Run the program: With Appletviewer: appleviewer HelloWorld With IE explorer: With Netscape: