introduction to java programming language instructor: an, seung hun [email protected]

26
Introduction to Java programming Language Instructor: An, Seung Hun [email protected]

Upload: robert-ball

Post on 23-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Introduction to Java programming Language

Instructor: An, Seung [email protected]

Page 2: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Plan of Java Tutorial• Part 1

– Basic Concept & Language Philosophy

• Part 2 & 3– Java Language Basics– Classes, Instances & Methods

• Part 4– Inheritance and Class hierachy

• Part 5– Exception & Package– Reserved

Page 3: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

What is Java?• What is Java?

– A simple, object oriented, distributed, interpreted, robust, safe, architecture neutral, portable, high performance, multithreaded, dynamic, programming language.

• Java is interesting because– It is both a general purpose object-oriented language

along the lines of C++, and– It is particularly designed to interface with Web pages

and to enable distributed applications over the Internet.

• The Web is becoming the dominant software development arena; this will drive Java as the best supported, most widely taught language.– Even outside the Web, e.g. in scientific computing, Java

is as good and in some respects better than other languages.

Page 4: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Why is Java good?• Several good design features

– secure, safe (w.r.t. bugs), object-oriented, familiar (to C, C++ and even Fortran programmers)

• good set of libraries covering everything from commerce, multimedia, images to math functions– see http://math.nist.gov/javanumerics

• naturally integrated with network and universal machine supports powerful “write once-run anywhere” model.

Page 5: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Architecture of Java Applications

• Java applications are compiled and run on a machine just like any other general programming language such as C/C++. No web server or network are required although Java applications may also use network connections for distributed computing

Java code

is compiledto produce

byte code

run by Javainterpreterto produceresults

Java code

is compiledto produce

native code

run directlyon machinefor betterperformance

Page 6: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Applications• Java programs written in a file with extension “.java”.

• Applications are .java files with a main() method. This is called by the Java system.

• Compile and run a Java application(using bytecodes):– Run the compiler on a .java file: javac MyProgram.java

producing a file of Java byte code, MyProgram.class – Run the interpreter on a .class file: java MyProgram which executes the byte code

• The tools javac and java are part of JDK.

Page 7: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

An Simple Example, Hello World!

• Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables

called fields, and subroutines called methods.

class HelloWorld { public static void main (String[] args) { System.out.println(“Hello World!”); }}

Each program is enclosed in a class definitionEach program is enclosed in a class definition..

main() is the first method that is run.main() is the first method that is run.main() is the first method that is run.main() is the first method that is run.

The notation class.method or The notation class.method or package.class.method is how package.class.method is how to refer to a public method to refer to a public method (with some exceptions).(with some exceptions).

Syntax is similar to C - Syntax is similar to C - braces for blocks, semicolon braces for blocks, semicolon after each statement. after each statement.

Page 8: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Applet• Java applets are classes written in Java that are

not intended to run as stand-alone programs (like applications) but as subprograms of a browser that is already managing a window.

• Applets are not trusted by default, so they have several restrictions in running on the client machine

• Applets should NOT have a main() method. Instead they have init(), start(), paint(), etc. for displaying on the browser window– no printing or file I/O– cannot connect through the network to any machine

but its own server– any new windows created by the applet have a

warning label

Page 9: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Architecture of Java Applets• Browsers (IE, Netscape, HotJava etc) supporting Java

allow arbitrarily sophisticated dynamic multimedia applications inserts called Applets, written in Java, to be embedded in the regular HTML pages and activated on each exposure of a given page.

web serverJava code

is compiledto produce

applet codes,part of webdocumentcollection

web client, running browsersuch as Netscape or IE

executes (restricted)applet code to displayin browser window

Internet

Page 10: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

An Simple Example, again• Java applets can call methods to display on a screen

(within the browser window). One way is to call the method drawString() from the standard method paint().

import java.awt.Graphics;

public class HelloApplet extends java.applet.Applet { public void paint (Graphics g) { g.drawString(“Hello World!”, 5, 25); }}

The The importimport statement (similar to anstatement (similar to an includeinclude) allows) allows the use of methods from thethe use of methods from the GraphicsGraphics class .class .

The The paint()paint() method displays a graphics method displays a graphics object on the screen - one of the standard object on the screen - one of the standard methods that takes the place of methods that takes the place of main()main() for for applets.applets.

Makes this a subclass of Makes this a subclass of Applet.Applet.

Page 11: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

On the Web• Name the file HelloWorldApplet.java. Run the compile

r, javac, to get a byte code file HelloWorldApplet.class. Put this in a web directory.

<html><head><title>Simple Hello Page</title></head><body>My Java applet says:<applet code=“HelloWorldApplet.class” width=150 height=25></applet></body></html>

Name of your applet class.Name of your applet class.

The browser will use a rectangle of width 150 pixels The browser will use a rectangle of width 150 pixels and height 25 pixels to display the applet within the and height 25 pixels to display the applet within the other html.other html.

Page 12: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java vs. JavaScript• JavaScript is a different language from Java,

albeit with some similarities.

• A JavaScript program is written in the HTML page, and executed by the JavaScript interpreter, so also allows dynamic web page content in the browser window.

• JavaScript is special purpose - it is an object-based language that deals directly with browser entities like windows, text fields, forms, frames and documents.

• JavaScript can respond to browser events like mouse clicks and user-typed text.

• JavaScript is fast to write, but not as powerful as Java.

Page 13: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Some Key Java Features First we discuss original Java base language features

as discussed in Java: A White Paper by Sun Microsystems—October 1995 draft by James Gosling and Henry McGilton—enumerates the original design goals of Java:

Object-oriented Architecture-neutral Portable Somewhat Interpreted Simple and FamiliarDistributed

Robust Secure High performance Multi ThreadedDynamic

Page 14: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Simple and Familiar

• Familiar as it looks like C++, but simpler to program.– omits several confusing features of C++ including

operator overloading, multiple inheritance, pointers and automatic type coercions

• Adds automatic garbage collection to make dynamic memory management much easier than in C or C++.– No more frees or deletes. No more memory leaks.

• Adds Interface construct, similar to Objective C concept, to compensate for the lack of multiple inheritance.

• Small kernel is suitable for Java ports to consumer electronic devices.

Page 15: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Object-oriented

• Java model is sometimes viewed as a C++ subset, with some elements imported from other languages.– This is arguable. In many ways Java and C++ are

very different, and many of the similarities that do exist are at a fairly superficial syntactic level.

• Structures, Unions and Functions are absorbed into data and methods of Java classes—Java is simple.

• The strength of Java object-oriented model is in simplicity and the extensive class library associated with the system.

Page 16: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Architecture-Neutral

• C/C++ programming in a heterogeneous network environment demands compatibility across several vendor platforms and their compilers.

• Solved in Java by designing platform-independent binary representation called Java bytecode—comparable to P-code in UCSD Pascal.

• Java compiler reads Java source and generates Java bytecode, which is shipped to user—e.g. on browser request, Jini lookup, etc.

• Each client must have a Java Virtual Machine program, which interprets (“runs”) Java bytecodes.

Page 17: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Portable

• Java Virtual Machine model is identical for all platforms.

• Sun “owns” the Java Virtual Machine specification—while classes can be added by any user, JVM is Universal.

• In C/C++ various integer types match the architecture of machine at hand. Java byte, char, short, int and long are always 8, 16 (unicode), 16, 32 and 64 bits, respectively.– No header files, preprocessors, #define etc.– floating point is always IEEE 754

Page 18: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Somewhat Interpreted

• Java represents a compromise between fully compiled (e.g. C/C++) and fully interpreted (e.g. typical scripting languages) models.

• Java “compiler” produces a binary bytecode output which is portable and typically smaller than the real binary for a specific machine. (Typical bytecode size is of order of the original source code, within a factor of 2).

• Java “interpreter”—the JVM—executes this bytecode.

Page 19: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Robust

• Java enforces compile-time type checking and this eliminates some error prone constructs of C/C++.

• Pointer arithmetic is eliminated which allows for, e.g., runtime checking of array subscripts, and enforces security of the Java model.

• Explicit declarations are always required; argument types of methods are always checked (unlike C). This allows the Java complier to perform early error detection.

Page 20: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's (Hopefully) Secure

• Java bytecodes may be shipped across the network and executed on client machines. Security is therefore a critical issue and strongly enforced in Java.

• The bytecodes sent across network are verified at the client which prevents evil/corrupted classes from causing problems

Page 21: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—High Performance

• Early Java interpreters performed on-the-fly execution of the Java bytecodes, which gave “moderate” performance. – Initial software was often 100 times slower than C

• Performance is improved in newer “just-in-time” JVMs, which compile methods after some number of executions, and save machine code to give compiled-code efficiency thereafter.

• Support for generating native machine code out of Java bytecodes also exists (e.g. TowerJ).

• The performance of the machine code, generated from Java bytecodes, may eventually be comparable to that offered by typical C/C++ compilers on the same platform.

Page 22: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Multithreaded

• Java model offers multithreading, implemented in terms of the Thread class.

• Thread methods offer a set of synchronization primitives based on monitor and condition variable paradigm of C.A.R. Hoare.– One use of Java multithreading in applet programming, for ex

ample, is having several independent but related simulations running concurrently in an applet window. Multithreading is also used internally by the browser to handle multiple document dynamics.

Page 23: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Features—It's Dynamic

• Java model is more dynamic than C++, closer to Smalltalk or Perl.

• Classes (often) don’t need to to be recompiled after implementation of a superclass (or other used class) is updated—binary compatibility.

• Classes have runtime representation (available through the Class class) that allows one, e.g., to discover and execute methods of a given object at runtime– In C, can’t even distinguish at run-time whether a given point

er references, say, an integer or a browser!

Page 24: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

The Java 2 Platform• Java 2 platform, Standard Edition (J2SE)

– Develops earlier JDKs

– Available in V 1.3.

• Java 2 platform, Enterprise Edition (J2EE)– Incorporates multiple technologies for server-side

and multi-tier applications.

• Java 2 platform, Micro Edition (J2ME)– Optimized run-time environment for consumer prod

ucts.

Page 25: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Development Environments

• These range from simple tools that give a windowing interface to the edit/compile/run or view cycle, e.g. JavaEdit from Dick Chase, on PC’s, . . .

• . . . to the elaborate commercial development environments that can also track projects and help generate code for user interface components– Microsoft Visual J++– Symantec Visual Café– Java Workshop from Sun– Borland Jbuilder– Kawa from Tek-Tools

Page 26: Introduction to Java programming Language Instructor: An, Seung Hun shahn@dcslab.snu.ac.kr

Java Books & References

• Core Java– Gary Cornell and Cay S. Horstmann

• Java Tutorial

• http://www.ibiblio.org/javafaq/course/index.html

• http://java.freehosting.co.kr/tutorial/

• http://aspen.csit.fsu.edu/it1spring01/