Download COMP201 Java Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP201 Java Programming
Topic 1: Introduction
Readings: Chapter 1
COMP201 Topic 1 / Slide 2
Outline

C, C++, and Java
Writing good codes in Java easier than in C++

Advantages of Java
Simple, Object-Oriented, Platform-Independent, Robust and Secure

How Java codes are executed
Java Virtual Machine, Portability, robustness, and security

Java libraries
Where the power of Java come from

Java and the internet
What Java is good for
COMP201 Topic 1 / Slide 3
C

C designed in 1970’s for operating system
programming (relatively low-level)

Strengths



Efficient
Not restrictive
Weakness


Expressiveness
Only a small amount of built-in functionality.
COMP201 Topic 1 / Slide 4
C, C++


C++ designed in 1980’s, complete superset of C
Changes




Strengths



Objects
Parameterized types
Unification of user-defined and built-in types
Improved data abstraction and encapsulation
More extensive built-ins (standard libraries)
Weakness


Considered by many to be over-complicated
Contains all of C’s warts
COMP201 Topic 1 / Slide 5
C, C++ and Java

Java: a language of 1990’s,

Similar to C/C++ but not a superset
Conscious design not to be backward compatible with C++, C.

Originally not designed as a general-purpose
programming language

Strong emphasis on safety and portability

Unique features make it particularly suitable for
dynamic content delivery over the net
COMP201 Topic 1 / Slide 6
C, C++ and Java

The design of Java starts with C syntax and semantics

Adds a few features from C++: Objects, exceptions

Leaves out parts unneeded, unsafe, complex
Gosling: “Java omits many rarely used, poorly understood,
confusing features of C++ that in our experience bring more
grief than benefits.”

Adds a few facilities not present in C or C++
Garbage collection, concurrency, runtime error checking

Strengthens portability and security
COMP201 Topic 1 / Slide 7
Advantages of Java

According to Sun’s Java White paper:
“Java is a simple, objected-oriented, distributed,
interpreted, robust, secure, architecture-neutral,
portable, high-performance, multi-threaded, and
dynamic languages”.

Most of the claims are justified, while others
are controversial.
COMP201 Topic 1 / Slide 8
Advantages of Java/Simple

Streamline C language:




Simplify problematic areas:



No typedefs, unions, enums, goto, comma operator
No preprocessor
No header files
No pointers, no function pointers
Add garbage collection. Memory leaks no more.
Provide better utility support:
Built-in array and string objects, hashtables, trees, dates, etc.

However, Java libraries are quite complicated
COMP201 Topic 1 / Slide 9
Advantages of Java/Object-Oriented

More so than C++.



Combine data and behavior into one unit, the object
Programs are collections of interacting, cooperating
objects
Perceived advantages of objects





Provide strong data abstraction and encapsulation
Gives framework for design
Allows independent development and testing
Facilitates system extensions and maintenance
More opportunity for code re-use
COMP201 Topic 1 / Slide 10
Advantages of Java/Platform-Independent

Java programs are compiled into bytecode, not object
code

Virtual machine interprets bytecodes during execution
(Different virtual machines for different platforms).

Also Java standardizes type sizes, byte ordering, order of
sub-expression evaluation, and behavior on exceptional
conditions

Consequently, Java programs can run on any platform.
“Write once, run anywhere”.
COMP201 Topic 1 / Slide 11
Advantages of Java/Robust and Secure

Fewer language loopholes
No pointers, typecasts limited.

Compilers are strict
Require initialization of variables, enforces type consistency, requires
prototypes

Runtime error checking
Array bounds, null pointer access

Security manager
System to control permissions for high-level actions

Runtime verifier checks untrusted bytecode
Avoids havoc from hand-constructed bytecode
COMP201 Topic 1 / Slide 12
Java Virtual Machine

Compile-interpret-execute cycle
Compile time environment
Class loader
bytecode to
verifier
Java
Source
Java
Compiler
Runtime environment
(Java Platform)
Bytecodes
move locally
or through net
Java
Interpreter
Just in Time
Compiler
Runtime System
Java
bytecode
(class)
Operating System
Hardware
Java
Libraries
Java
Virtual
Machine
COMP201 Topic 1 / Slide 13
Java Virtual Machine


Virtual machine (VM) is the execution environment for
java code

Java source is compiled into bytecodes

Bytecodes are instructions for virtual machines

VM is a platform-specific tool to interpret bytecodes and
translate them into commands for certain processor and OS
Porting a VM (and libraries) is the hard part
But porting is done once by the vendor instead of again and
again by each application author
COMP201 Topic 1 / Slide 14
Java Virtual Machine

Execution occurs in “artificial” context, not on raw
hardware.


Bytecodes are verified before execution


Portability: write once, run everywhere.
Ensures security and correctness
VM adds inefficiency

Extra work to interpret bytecodes as well as safety checks
Interpreted bytecode can be 5-10 times slower than compiled C

“Just in time” (JIT) code generator can compile bytecodes to
machine code on the fly
has brought us within factor of 2-3 of C

Sun’s next generation “HotSpot” VM with aggressive code
optimization and compilation can be as fast as C for CPUintensive loops
COMP201 Topic 1 / Slide 15
Java Libraries

Java has far expanded traditional scope of a
language’s libraries

Much less effort required to accomplish common tasks

Platform-specific details are handled

All Programs improve when common core updated
COMP201 Topic 1 / Slide 16
Java Libraries

Collection of classes grouped into packages
Java equivalent of C libraries

java.lang
String, Math, Exception, Thread, Runtime, etc

java.util
Vector, Stack, hashtable, Date, Tokenizer

java.io
Varieties of input/output processing

java.net
Networking, client/server sockets, URLs

java.awt, javax.swing
Windows, buttons, drawing, images, events
COMP201 Topic 1 / Slide 17
Java Libraries

java.security
Encryption, digital signature, message digest

java.text
Formatting and parsing

java.sql
Database connectivity

java.rmi
Remote method invocation, distributed objects

The list goes on…
And it is continuing to expand. Java is still young
COMP201 Topic 1 / Slide 18
Java and the Internet

Java is intended to be used in networked/distributed
environments.

Increasingly used for “middleware” to communicate between
clients and serves, acting as a universal glue that connect user with
information from various sources.


Applets are java programs that works on webpages for dynamic
content delivery. (E.g. Jmol applet)


Made possible by portability and multithreading and networking capabilities.
Useful especially on intranets for things such as inventory checking,
travel reimbursement, … etc.
Servlets are for server-side programming, better in many ways
than alternatives such as Perl. Example: Duke’s book store.