1 / 18

COMP201 Java Programming

COMP201 Java Programming. Topic 1: Introduction Readings: Chapter 1. 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

beaman
Télécharger la présentation

COMP201 Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP201 Java Programming Topic 1: Introduction Readings: Chapter 1

  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

  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.

  4. C, C++ • C++ designed in 1980’s, complete superset of C • Changes • Objects • Parameterized types • Unification of user-defined and built-in types • Strengths • 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

  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

  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

  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.

  8. Advantages of Java/Simple • Streamline C language: • No typedefs, unions, enums, goto, comma operator • No preprocessor • No header files • Simplify problematic areas: • 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

  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

  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”.

  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

  12. Java Interpreter Java Compiler Java Libraries Just in Time Compiler Runtime System Operating System Hardware Java Virtual Machine Compile-interpret-execute cycle Runtime environment (Java Platform) Compile time environment Class loader bytecode to verifier Java Source Java Virtual Machine Bytecodes move locally or through net Java bytecode (class)

  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

  14. Java Virtual Machine • Execution occurs in “artificial” context, not on raw hardware. • Portability: write once, run everywhere. • Bytecodes are verified before execution • 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 CPU-intensive loops

  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

  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

  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

  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. • Made possible by portability and multithreading and networking capabilities. • Applets are java programs that works on webpages for dynamic content delivery. (E.g. Jmol applet) • 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.

More Related