Java Tutorial Ethan Cerami @1998 New York University.

29
Java Java Tutorial Tutorial Ethan Cerami Ethan Cerami @1998 New York University @1998 New York University

Transcript of Java Tutorial Ethan Cerami @1998 New York University.

Page 1: Java Tutorial Ethan Cerami @1998 New York University.

Java TutorialJava TutorialEthan CeramiEthan Cerami

@1998 New York University@1998 New York University

Page 2: Java Tutorial Ethan Cerami @1998 New York University.

Road MapRoad Map

What is Java?What is Java? Why is Java Important?Why is Java Important?

– Strengths, WeaknessesStrengths, Weaknesses Java Tutorial: Creating an AppletJava Tutorial: Creating an Applet Cool Examples/DemosCool Examples/Demos Future of JavaFuture of Java

Page 3: Java Tutorial Ethan Cerami @1998 New York University.

I. What is Java?I. What is Java?

Java is a new Internet programming Java is a new Internet programming language.language.

Developed by Sun Microsystems, Developed by Sun Microsystems, Inc.Inc.

Originally developed for Interactive Originally developed for Interactive Television Set-top boxes.Television Set-top boxes.

Rapidly becoming the standard for Rapidly becoming the standard for Internet programming.Internet programming.

Page 4: Java Tutorial Ethan Cerami @1998 New York University.

Evolution of the WebEvolution of the Web

Phase 1: Everything was in text. Used Phase 1: Everything was in text. Used mainly by academics and government.mainly by academics and government.

Phase 2: Simple, graphical user Phase 2: Simple, graphical user interface.interface.– Mosaic and Netscape. Mosaic and Netscape. – Used by millions of people.Used by millions of people.

Phase 3: Interactive Applications and Phase 3: Interactive Applications and Web Pages.Web Pages.– Where we are today.Where we are today.

Page 5: Java Tutorial Ethan Cerami @1998 New York University.

AppletsApplets

Applets: mini Java applications Applets: mini Java applications that can be embedded inside a that can be embedded inside a web page.web page.

Clock Applet

Big TextSmaller Text

Page 6: Java Tutorial Ethan Cerami @1998 New York University.

Some Basic ExamplesSome Basic Examples

Scrolling TickerScrolling Ticker ““Bouncing Heads”Bouncing Heads” Simple Video GamesSimple Video Games

Page 7: Java Tutorial Ethan Cerami @1998 New York University.

II. Why is Java Important?II. Why is Java Important?

Enables the distribution of applications Enables the distribution of applications across the Internet.across the Internet.

As a language, Java also has many As a language, Java also has many important strengths:important strengths:– Object OrientedObject Oriented– Built-in SecurityBuilt-in Security– Built-in NetworkingBuilt-in Networking– Platform IndependentPlatform Independent– Built-in MultithreadingBuilt-in Multithreading

Page 8: Java Tutorial Ethan Cerami @1998 New York University.

Feature #1: Object Feature #1: Object OrientedOriented

Object Oriented languages enable Object Oriented languages enable the creation of large applications the creation of large applications that are easy to maintain.that are easy to maintain.

Clock Object

ScrollingTickerObject

WeatherObject

Page 9: Java Tutorial Ethan Cerami @1998 New York University.

Feature #2: Built-In Feature #2: Built-In SecuritySecurity

If you download an application If you download an application across the Internet, the program across the Internet, the program might do malicious things: might do malicious things: deleting files, reading sensitive deleting files, reading sensitive data, etc.data, etc.

.

Operating System: Read, write, delete files

Java Security SandboxRestricted Access

Page 10: Java Tutorial Ethan Cerami @1998 New York University.

Feature #3: NetworkingFeature #3: Networking

It is extremely easy to create It is extremely easy to create network applications in Java.network applications in Java.

Java was built from the ground up Java was built from the ground up to work with the Internet.to work with the Internet.

Page 11: Java Tutorial Ethan Cerami @1998 New York University.

Feature #4: PortableFeature #4: Portable

Java is Platform independent: Java is Platform independent: Runs on any platform - Windows, Runs on any platform - Windows, Macintosh, UNIX.Macintosh, UNIX.

““Write once, run anywhere.”Write once, run anywhere.” Create a single application and it Create a single application and it

can run anywhere on the Internet.can run anywhere on the Internet.

Page 12: Java Tutorial Ethan Cerami @1998 New York University.

Feature #5: Feature #5: MultithreadingMultithreading

You can create applications that do You can create applications that do more than one thing at a time.more than one thing at a time.

For example, you can have an For example, you can have an animation and a sound at the animation and a sound at the same time.same time.

Important for animations and Important for animations and network applications.network applications.

Page 13: Java Tutorial Ethan Cerami @1998 New York University.

Java Virtual MachineJava Virtual Machine

C/C++Program

OperatingSystem

Java VirtualMachine

OperatingSystem

JavaApplet

Page 14: Java Tutorial Ethan Cerami @1998 New York University.

Java PerformanceJava Performance

The Achilles Heal of JavaThe Achilles Heal of Java When Java was first released, it When Java was first released, it

was about 20 times slower than was about 20 times slower than native applications written in C/C+native applications written in C/C++.+.

Now, it’s about half the speed of Now, it’s about half the speed of C/C++. But, it really depends on C/C++. But, it really depends on the Browser and the Platform.the Browser and the Platform.

Page 15: Java Tutorial Ethan Cerami @1998 New York University.

III. Java TutorialIII. Java Tutorial

Once you understand the basics of Once you understand the basics of Java, creating an Applet is Java, creating an Applet is straightforward.straightforward.

In order to create an applet, you In order to create an applet, you first need to download a Java first need to download a Java Compiler.Compiler.– Download the Java Development Kit Download the Java Development Kit

(JDK) 1.1 from java.sun.com(JDK) 1.1 from java.sun.com

Page 16: Java Tutorial Ethan Cerami @1998 New York University.

Object Oriented BasicsObject Oriented Basics

Every object has two things:Every object has two things:– Some DataSome Data– Some Methods (or functions)Some Methods (or functions)

For example, a scrolling ticker For example, a scrolling ticker object might have:object might have:– The Text to be displayed.The Text to be displayed.– A method called animate() that tells A method called animate() that tells

the object to start scrolling.the object to start scrolling.

Page 17: Java Tutorial Ethan Cerami @1998 New York University.

The Applet ObjectThe Applet Object

The Applet objects contains:The Applet objects contains:– Data regarding the Web Browser.Data regarding the Web Browser.– Methods for creating applications.Methods for creating applications.

Some methods:Some methods:– getImage ():getImage (): Retrieves an imageRetrieves an image– paint ():paint (): Paint on a canvasPaint on a canvas– getAudioClip():getAudioClip(): Retrieves a sound fileRetrieves a sound file

Page 18: Java Tutorial Ethan Cerami @1998 New York University.

Hello World!Hello World!

import java.applet.*;import java.awt.*;

// This applet prints Hello public class FirstApplet extends Applet {

// The method paints the applet public void paint (Graphics g) {

g.drawString ("Hello, World!", 25, 50);}

}

Page 19: Java Tutorial Ethan Cerami @1998 New York University.

Import/CommentsImport/Comments

Import statement tells the Compiler to Import statement tells the Compiler to load certain libraries:load certain libraries:– similar to the #include statement in C.similar to the #include statement in C.– import java.applet.*;import java.applet.*;– import java.awt.*; (Abrstract Windowing import java.awt.*; (Abrstract Windowing

Toolkit. Useful for using widgets and Toolkit. Useful for using widgets and graphics)graphics)

Comments are denoted with //Comments are denoted with //– // This is a comment.// This is a comment.

Page 20: Java Tutorial Ethan Cerami @1998 New York University.

Extending AppletExtending Applet

public class FirstApplet extends public class FirstApplet extends Applet {Applet {

This tells the compiler that you This tells the compiler that you want to create an Applet object. want to create an Applet object. Called FirstApplet.Called FirstApplet.

This really relates to object This really relates to object oriented inheritance (which we oriented inheritance (which we don’t have time to discuss.)don’t have time to discuss.)

Page 21: Java Tutorial Ethan Cerami @1998 New York University.

Drawing GraphicsDrawing Graphics

paint method:paint method:– Responsible for all graphics painting Responsible for all graphics painting

inside the applet.inside the applet. g.drawString (“Hello World”, 25, g.drawString (“Hello World”, 25,

50);50);

X CoordinateY Coordinate

Page 22: Java Tutorial Ethan Cerami @1998 New York University.

To View your AppletTo View your Applet

First you need to compile your First you need to compile your applet:applet:– javac FirstApplet.javajavac FirstApplet.java

Then, you need to create a simple Then, you need to create a simple HTML page:HTML page:

<APPLET code="FirstApplet.class" width=150 height=100></APPLET>

Page 23: Java Tutorial Ethan Cerami @1998 New York University.

Hello World, Version 2.0Hello World, Version 2.0

import java.applet.*;import java.awt.*;

// This applet prints Hello, Version 2.0 public class SecondApplet extends Applet {

// The method paints the applet public void paint (Graphics g) {

g.setColor (Color.pink);g.fillOval (10,10,330,100);g.setColor (Color.black);g.drawOval (10,10,328,100);g.setFont (new Font("Helvetica", Font.BOLD, 48));g.drawString ("Hello, World", 40, 75);

}}

Page 24: Java Tutorial Ethan Cerami @1998 New York University.

To View the Second AppletTo View the Second Applet

HTML Page:HTML Page:

<APPLET code="SecondApplet.class" width=350 height=125></APPLET>

Page 25: Java Tutorial Ethan Cerami @1998 New York University.

IV. Cool ExamplesIV. Cool Examples The Molecule Viewer:The Molecule Viewer:

– http://java.sun.com/nav/whatis/cherwell.htmlhttp://java.sun.com/nav/whatis/cherwell.html

Wired Stock Graphing Tool:Wired Stock Graphing Tool:– http://stocks.wired.comhttp://stocks.wired.com

3D Ray:3D Ray:– http://www.2nu.com/Wayne/SplattJava/http://www.2nu.com/Wayne/SplattJava/

SplattJava480x300.htmlSplattJava480x300.html Games:Games:

– http://www.comedycentral.com/southpark/http://www.comedycentral.com/southpark/cartman/cartman.htmcartman/cartman.htm

Page 26: Java Tutorial Ethan Cerami @1998 New York University.

The Future of JavaThe Future of Java

To answer this question, you have To answer this question, you have to look at two elements:to look at two elements:– What can you do with Java? What What can you do with Java? What

kind of added functionality will we kind of added functionality will we see?see?

– Where can you actually run Java? Where can you actually run Java? What comes after the Applet?What comes after the Applet?

Page 27: Java Tutorial Ethan Cerami @1998 New York University.

Java 1.2Java 1.2

Current Release: Java 1.2 (Beta) Current Release: Java 1.2 (Beta) Most browsers only support Most browsers only support Version 1.0Version 1.0

What’s new in Java 1.2:What’s new in Java 1.2:– Java Foundation Classes (Swing)Java Foundation Classes (Swing)– Java 2D, 3DJava 2D, 3D– Java SoundJava Sound– Java SpeechJava Speech

Page 28: Java Tutorial Ethan Cerami @1998 New York University.

Java EverywhereJava Everywhere

Java can run on any platform.Java can run on any platform. Embedded Java:Embedded Java:

– Java RingsJava Rings– Java Smart CardsJava Smart Cards– Java TelephonesJava Telephones– Java TelevisionsJava Televisions– Java light switchesJava light switches

Page 29: Java Tutorial Ethan Cerami @1998 New York University.

One Java or Many?One Java or Many?

Sun wants Java to be an open Sun wants Java to be an open standard that can run on any standard that can run on any platform.platform.

Microsoft hates Java. Has created Microsoft hates Java. Has created a slightly different version that is a slightly different version that is optimized for Windows.optimized for Windows.