27
-1 Copyright (c) 1999 N. Afsharto Introduction to Java™

1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-1 Copyright (c) 1999 N. Afshartous

Introduction to Java™

Page 2: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-2 Copyright (c) 1999 N. Afshartous

Version 3.0

Copyright (c) 1997,1998,1999 N Afshartous - All rights reserved. This product and related documentation are protected by copyright and distributed under licenses restricting its use, copying, distribution, decompilation. No part of this product or related documentation may be reproduced in any form by any means without prior written authorization.

Trademarks: Java, JDK, Java Development Kit are trademarks of Sun Microsystems. Mosaic, NCSA Mosaic are proprietary trademarks of the University of Illinois.

Disclaimer: Although great care has been taken to ensure the accuracy and quality of these materials, all material is provided without any warranty whatsoever, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose.

Page 3: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-3 Copyright (c) 1999 N. Afshartous

About the author: Niki Afshartous has been teaching corporate technical training courses for five years. Previously he was at the MIT Lincoln Laboratory as a systems programmer. He holds a Ph.D. degree in computer science from New York University where his research focus was on concurrent programming languages.

Dedication: For Jenny

Acknowledgement: Thanks to Maki Takeuchi for carefully proof reading earlier versions of this course.

Comments, questions ? Don’t hesitate to contact the author.

Email: [email protected]

WWW: www.cs.nyu.edu/afshar

Page 4: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-4 Copyright (c) 1999 N. Afshartous

Contents1. Introduction

2. Types and Variables

3. Statements and Control Flow

4. Reading Input

5. Classes and Objects

6. Arrays

7. Methods

8. Scope and Lifetime

9. Utility classes

10. Introduction to Object-Oriented Analysisand Design

Page 5: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-5 Copyright (c) 1999 N. Afshartous

Chapter 1: Introduction

After this chapter you will be able to:

- Describe the history of the Web

- Identify the impact of Java™ on the Web

- List the design goals of the Java™ team

Page 6: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-6 Copyright (c) 1999 N. Afshartous

History of WWW

• (Early nineties) Web was invented by Tim Berners-Lee who applied hypertext to make the internet user-friendly

• (1994) Most people were using the non-commercial web-browser Mosaic. Mosaic was partially developed by Marc Andressen for $6.85/hr as an undergrad on work-study.

• Later Andressen co-founded Netscape

• (1995) Microsoft holds a major press conference to announce an Internet strategy

Page 7: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-7 Copyright (c) 1999 N. Afshartous

Architecture of WWW

Web serverclient

BrowserHTML Images CGI scripts

Client

• HTML - Hypertext Markup Language

• CGI - Common Gateway Interface

• WWW limitation: only server side executionusing CGI.

Page 8: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-8 Copyright (c) 1999 N. Afshartous

Enter Java™

• Sun introduced Java™ at the Sun World conference on May 25, 1995

• Java™ makes client-side execution on the Web possible !

Page 9: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-9 Copyright (c) 1999 N. Afshartous

Architecture of WWW + Java™ Web server client

HTML Java Bytecode

CGI scripts

Client

• A Web page can be downloaded along with Java™ bytecode

• The bytecode is executed by a virtual machine embedded inside the web-browser

ImagesBrowser +Virtual Machine

Page 10: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-10 Copyright (c) 1999 N. Afshartous

Javascript

• Netscape subsequently released Javascript (originally called Livescript)

• The Java™ name was licensed from Sun Micro

• Javascript also facilitates client-side execution on the web

• Javascript is essentially an extension of HTML

Page 11: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-11 Copyright (c) 1999 N. Afshartous

Architecture of WWW + Javascript

Web server client

HTML +Javascript

CGI scripts

Client

• A Web page can contain both HTML and Javascript

• The Javascript code is executed by an interpreter embedded inside the web-browser

Images

Browser +Javascript Interpreter

Page 12: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-12 Copyright (c) 1999 N. Afshartous

Java™ .............VS........Javascript

• Created by Sun Micro

• Can be used stand-alone or with the web

• Source compiled into bytecode

• Object-oriented

• Has facilities to support large applications

• Variables and expressions are statically typed

• Has threads for concurrent programming

• Fast exec but slow download

• Created by Netscape

• Tied to the web

• Source directly interpreted

• Object-based (no inheritance)

• No facilities for large applications

• Variables and expressions are dynamically typed

• No threads

• Slow exec but fast download

Page 13: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-13 Copyright (c) 1999 N. Afshartous

Summary of Java™ and Javascript

• Java™ is a powerful general purpose programming language– web server– web client– stand-alone

• Javascript facilitates making web pages more dynamic– web client– Common application is validating user entered form

data

Page 14: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-14 Copyright (c) 1999 N. Afshartous

Java™ Design Goals

• Simple

• Object-oriented

• Distributed

• Robust and secure

• Architecture neutral and portable

• Interpreted

• High-performance

• Multithreaded

• Dynamic

Page 15: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-15 Copyright (c) 1999 N. Afshartous

Simple

• Java™ designed as close as possible to C++ to look familiar

• Java™ omits confusing and complex features of C++– operator overloading– multiple inheritance– extensive automatic coercion's

• Java™ incorporates:– garbage collection

TM

Page 16: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-16 Copyright (c) 1999 N. Afshartous

Object-oriented

• Focuses design on data and the operations on the data

• Helps to provide reusable software components

Page 17: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-17 Copyright (c) 1999 N. Afshartous

Distributed

• Java™ has library support for TCP/IP protocols– http– ftp

• Java™ applications can access remote objects by using URLs

• JDBC (Java Database Connectivity) kit allows access to remote databases

Page 18: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-18 Copyright (c) 1999 N. Afshartous

Architecture neutral

• Java™ compiler generates an architecture neutral object file format (bytecode)

• Bytecode can be executed on any platform where there is a virtual machine

Page 19: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-19 Copyright (c) 1999 N. Afshartous

Robust and secure

• Compile-time type checking

• Run-time checking (i.e. array bounds)

• Hard to write viruses in Java™ (no pointers)

• Bytecode is verified before it is run

Page 20: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-20 Copyright (c) 1999 N. Afshartous

Interpreted

• Java™ bytecode executed by an interpreter (virtual machine VM)

• VM can be stand-alone or embedded inside a web browser

• Other interpreted languages– scripts– Lisp– SmallTalk– Basic (Visual Basic)

Page 21: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-21 Copyright (c) 1999 N. Afshartous

Interpreted cont,

bytecode

(.class)

source

(.java)

interpreter(virtual machine)

compiler

Page 22: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-22 Copyright (c) 1999 N. Afshartous

High-performance

• Interpreting bytecode is more efficient than interpreting a source file (script).

• Just-in-time compilation (JIT) makes Java™ even faster. A virtual machine with JIT will translate bytecode into native executable format.

Page 23: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-23 Copyright (c) 1999 N. Afshartous

Multithreaded

• Java™ has threads and primitives for synchronization.

• Having threads facilitates writing programs that incorporate animation.

Page 24: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-24 Copyright (c) 1999 N. Afshartous

Dynamic

• Java™ was designed to adapt to an evolving environment.

• New code can be dynamically linked into a running system.

Page 25: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-25 Copyright (c) 1999 N. Afshartous

Free !

• JDK™ - Java Development Kit available from Sun Microsystems (http://www.javasoft.com)

• JDK™ consists of:– javac:compiler– java: virtual machine (bytecode interpreter)– javadoc: a program that generates HTML doc from

source code comments– other miscellaneous programs

Page 26: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-26 Copyright (c) 1999 N. Afshartous

Terms

• Applet - A Java™ program that executes inside a web browser

• Servlet - A Java™ program that executes on a web server

• Javabean - a Java™ object which may be graphically manipulated

Page 27: 1-1 Copyright (c) 1999 N. Afshartous Introduction to Java™

1-27 Copyright (c) 1999 N. Afshartous

References

• The Java Language: An Overview (a.k.a.“The Java™ Whitepaper”)at http://www.javasoft.com