Intro to java API.ppt

Embed Size (px)

Citation preview

  • 8/10/2019 Intro to java API.ppt

    1/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:1

    Introduction to Java API

  • 8/10/2019 Intro to java API.ppt

    2/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:2

    What is Java?

    Java (with a capital J) is a high-level, third generation

    programming language, like C, Fortran, Smalltalk, Perl,and many others.

    You can use Java to write computer applications thatcrunch numbers, process words, play games, store data

    or do any of the thousands of other things computersoftware can do.

    Compared to other programming languages, Java is mostsimilar to C. However although Java shares much of C's

    syntax, it is not C. Knowing how to program in C or,better yet, C++, will certainly help you to learn Javamore quickly, but you don't need to know C to learnJava. Unlike C++ Java is not a superset of C.

  • 8/10/2019 Intro to java API.ppt

    3/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:3

    What is Java A Java compiler won't compile C code, and most large C

    programs need to be changed substantially before theycan become Java programs.

    What's most special about Java in relation to other

    programming languages is that it lets you write specialprograms called appletsthat can be downloaded fromthe Internet and played safely within a web browser.

    A Java applet cannot write to your hard disk without

    your permission. It cannot write to arbitrary addressesin memory and thereby introduce a virus into yourcomputer. It should not crash your system.

  • 8/10/2019 Intro to java API.ppt

    4/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:4

    What Is the JavaTechnology?

    Java technology is:

    A programming language

    A development environment

    An application environment

    A deployment environment It is similar in syntax to C++.

    It is used for developing both applets and applications.

  • 8/10/2019 Intro to java API.ppt

    5/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:5

    Primary Goals of the Java Technology

    Provides an easy-to-use language by:

    Avoiding many pitfalls of other languages

    Being object-oriented

    Enabling users to create streamlined and clear code

    Provides an interpreted environment for:

    Improved speed of development Code portability

    Enables users to run more than one thread of activity

    Loads classes dynamically; that is, at the time they are actually

    needed

    Supports changing programs dynamically during runtime by loading

    classes from disparate sources

    Furnishes better security

  • 8/10/2019 Intro to java API.ppt

    6/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:6

    Primary Goals of the Java Technology

    The following features fulfill these goals:

    The Java Virtual Machine (JVM)1

    Garbage collection

    The Java Runtime Environment (JRE)

    JVM tool interface

  • 8/10/2019 Intro to java API.ppt

    7/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:7

    A Brief History

    January 1996: first official release JDK 1.0 Web: applets, security, URL, networking

    GUI: Abstract Windows Toolkit (AWT)

    February 1997: JDK 1.1 Authentication: digital signatures, certificates

    Distributed computing: RMI, object serialization, Java

    IDL/CORBA Database connectivity: JDBC

    Component architecture: JavaBean

  • 8/10/2019 Intro to java API.ppt

    8/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:8

    A Brief History (contd)

    December 1998: Java 2 Platform (JDK 1.2): Standard (J2SE), Enterprise (J2EE), and Micro (J2ME) Editions

    JFC: Swing, Java2D, Java3D

    Java Cryptography Extension (JCE) Enterprise computing: enterprise JavaBean (EJB), servlets,

    Java server page (JSP), Jini, XML

    Java Multimedia Framework (JMF)

    Embedded systems: KVM, JavaCard

    Performance enhancement: JIT, HotSpot VM

  • 8/10/2019 Intro to java API.ppt

    9/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:9

    A Brief History (contd)

    May 2000: J2SDK v1.3 Enhancements in features, performance, and security.

    May 2001: J2SDK v1.4 beta

    XML, XSLT, JCE, etc. all included in J2SE

    Numerous enahnacements: I/O, assertions, regular

    expression, logging, etc.

    2002(?): J2SDK v1.5

    Generic types

  • 8/10/2019 Intro to java API.ppt

    10/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:10

    Java is a Platform

    Java (with a capital J) is a platform for application development.

    A platform is a loosely defined computer industry buzzword thattypically means some combination of hardware and system softwarethat will mostly run all the same software.

    Java solves the problem of platform-independence by using byte code.The Java compiler does not produce native executable code for aparticular machine like a C compiler would. Instead it produces aspecial format called byte code. Java byte code written inhexadecimal, byte by byte, looks like this:

    CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

    This looks a lot like machine language, but unlike machine languageJava byte code is exactly the same on every platform. This byte codefragment means the same thing on a Solaris workstation as it does on aMacintosh PowerBook.

  • 8/10/2019 Intro to java API.ppt

    11/27

  • 8/10/2019 Intro to java API.ppt

    12/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:12

    Java is Simple Java was designed to make it much easier to write bug free code.

    Java has the bare bones functionality needed to implement its richfeature set. It does not add lots of syntactic sugar or unnecessaryfeatures. Despite its simplicity Java has considerably more functionalitythan C, primarily because of the large class library.

    Because Java is simple, it is easy to read and write. Obfuscated Java isn'tnearly as common as obfuscated C. There aren't a lot of special cases ortricks that will confuse beginners.

    About half of the bugs in C and C++ programs are related to memory

    allocation and deallocation. Therefore the second important additionJava makes to providing bug-free code is automatic memory allocationand deallocation. The C library memory allocation functions malloc() andfree() are gone as are C++'s destructors.

  • 8/10/2019 Intro to java API.ppt

    13/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:13

    Java is Simple

    Java is an excellent teaching language, and anexcellent choice with which to learn programming.

    The language is small so it's easy to become fluent.The language is interpreted so the compile-run-linkcycle is much shorter.

  • 8/10/2019 Intro to java API.ppt

    14/27

  • 8/10/2019 Intro to java API.ppt

    15/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:15

    Java is Object-Oriented

    In practice object-oriented programs have been just as

    slow, expensive and buggy as traditional non-object-

    oriented programs.

    In large part this is because the most popular object-oriented language is C++. C++ is a complex, difficult

    language that shares all the obfuscation of C while

    sharing none of C's efficiencies. It is possible in practiceto write clean, easy-to-read Java code.

  • 8/10/2019 Intro to java API.ppt

    16/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:16

    Java is Platform Independent

    Java was designed to not only be cross-platform in source form like C, but

    also in compiled binary form.

    Since this is frankly impossible across processor architectures Java iscompiled to an intermediate form called byte-code.

    A Java program never really executes natively on the host machine.Rather a special native program called the Java interpreter reads thebyte code and executes the corresponding native machine instructions.

    Thus to port Java programs to a new platform all that is needed is to port

    the interpreter and some of the library routines.

    Even the compiler is written in Java. The byte codes are preciselydefined, and remain the same on all platforms.

  • 8/10/2019 Intro to java API.ppt

    17/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:17

    Java is Platform Independent

    The second important part of making Java cross-platform is the

    elimination of undefined or architecture dependent constructs. Integers are always four bytes long, and floating point variables

    follow the IEEE 754 standard for computer arithmetic exactly.

    You don't have to worry that the meaning of an integer is going

    to change if you move from a Pentium to a PowerPC. In Java everything is guaranteed.

    However the virtual machine itself and some parts of the classlibrary must be written in native code.

    These are not always as easy or as quick to port as pure Javaprograms. This is why for example, there's not yet a version ofJava 1.2 for the Mac.

  • 8/10/2019 Intro to java API.ppt

    18/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:18

    Java is High Performance

    Java byte codes can be compiled on the fly to code that rivals C++ in

    speed using a "just-in-time compiler." Several companies are alsoworking on native-machine-architecture compilers for Java. These willproduce executable code that does not require a separate interpreter,and that is indistinguishable in speed from C++.

    While you'll never get that last ounce of speed out of a Java program

    that you might be able to wring from C or Fortran, the results will besuitable for all but the most demanding applications.

    It is certainly possible to write large programs in Java. The HotJavabrowser, the Eclipse integrated development environment, the

    LimeWire file sharing application, the jEdit text editor, the JBossapplication server, the Tomcat servlet container, the Xerces XMLparser, the Xalan XSLT processor, and the javac compiler are largeprograms that are written entirely in Java.

  • 8/10/2019 Intro to java API.ppt

    19/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:19

    Java is Multi-Threaded Java is inherently multi-threaded. A single Java program can have many

    different threads executing independently and continuously. Three Javaapplets on the same page can run together with each getting equal timefrom the CPU with very little extra effort on the part of theprogrammer.

    This makes Java very responsive to user input. It also helps tocontribute to Java's robustness and provides a mechanism whereby theJava environment can ensure that a malicious applet doesn't steal all ofthe host's CPU cycles.

    Unfortunately multithreading is so tightly integrated with Java, that itmakes Java rather difficult to port to architectures like Windows 3.1 orthe PowerMac that don't natively support preemptive multi-threading.

    There is a cost associated with multi-threading. Multi-threading is toJava what pointer arithmetic is to C, that is, a source of devilishly hardto find bugs. Nonetheless, in simple programs it's possible to leavemulti-threading alone and normally be OK.

  • 8/10/2019 Intro to java API.ppt

    20/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:20

    Java is Dynamic(ly linked) Java does not have an explicit link phase. Java source code is divided

    into .java files, roughly one per each class in your program. The compiler

    compiles these into .class files containing byte code. Each .java file

    generally produces exactly one .class file.

    The compiler searches the current directory and directories specified in

    the CLASSPATH environment variable to find other classes explicitly

    referenced by name in each source code file. If the file you're compiling

    depends on other, non-compiled files the compiler will try to find them

    and compile them as well. The compiler is quite smart, and can handle

    circular dependencies as well as methods that are used before they're

    declared. It also can determine whether a source code file has changed

    since the last time it was compiled.

  • 8/10/2019 Intro to java API.ppt

    21/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:21

    More importantly, classes that were unknown to a program

    when it was compiled can still be loaded into it at runtime.

    For example, a web browser can load applets of differing

    classes that it's never seen before without recompilation.

    Furthermore, Java .class files tend to be quite small, a few

    kilobytes at most. It is not necessary to link in large runtime

    libraries to produce a (non-native) executable. Instead the

    necessary classes are loaded from the user's CLASSPATH.

  • 8/10/2019 Intro to java API.ppt

    22/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:22

    Java is Garbage Collected

    You do not need to explicitly allocate or deallocate memory in

    Java. Memory is allocated as needed, both on the stack and theheap, and reclaimed by thegarbage collectorwhen it is no longerneeded. There's no malloc(), free(), or destructor methods.

    There are constructors and these do allocate memory on the heap,

    but this is transparent to the programmer.

    The exact algorithm used for garbage collection varies from onevirtual machine to the next. The most common approach inmodern VMs is generational garbage collection for short-lived

    objects, followed by mark and sweep for longer lived objects. Ihave never encountered a Java VM that used reference counting.

  • 8/10/2019 Intro to java API.ppt

    23/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:23

    First Java Program

    class HelloJava

    {

    public static void main (String args[])

    {

    System.out.println("Hello Java!");

    }

    }

  • 8/10/2019 Intro to java API.ppt

    24/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:24

    First Java Program

    To write the code you need a text editor. You can use any

    text editor like Notepad, Brief, emacs or vi. Personally I useBBEdit on the Mac and TextPad on Windows.

    When you've chosen your text editor, type or copy the aboveprogram into a new file. For now type it exactly as it appears

    here. Like C and unlike Fortran, Java is case sensitive soSystem.out.println is not the same as system.out.println.CLASS is not the same as class, and so on.

    Assuming that Java is properly installed on your system there

    are three steps to creating a Java program: writing the code compiling the code running the code

  • 8/10/2019 Intro to java API.ppt

    25/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:25

    First Java Program Under Windows, it's similar. You just do this in a DOS

    shell.

    C:> javac HelloWorld.java

    C:> java HelloWorld

    Hello WorldC:>

    Notice that you use the .java extension when

    compiling a file, but you do not use the .classextension when running a file.

    For IDEs, consult your product documentation.

  • 8/10/2019 Intro to java API.ppt

    26/27

    Copyrights 2009BVU Amplify DITM

    Core Java

    Page:26

    First Java Program

    Under Windows you set the CLASSPATH environment variable

    with a DOS command like

    C:\> SET CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

    You can also add this to your autoexec.bat file (Windows ME and

    earlier) or set it in the environment variables tab of the Systemcontrol panel (Windows NT and later) You should of course pointit at whichever directories actually contain your classes.

    The CLASSPATH variable is also important when you run Javaapplets, not just when you compile them. It tells the webbrowser or applet viewer where it should look to find thereferenced .class files. If the CLASSPATH is set improperly, you'llprobably see messages like "Applet could not start."

  • 8/10/2019 Intro to java API.ppt

    27/27

    Copyrights 2009

    Core Java

    Page:27

    If the CLASSPATH environment variable has not been set, and you do

    not specify one on the command line, then Java sets the CLASSPATH to

    the default:

    Unix: .:$JAVA/classes:$JAVA/lib/classes.zip

    Windows: .:$JAVA\classes:$JAVA\lib\classes.zip

    Mac: ./$JAVA:classes/$JAVA:lib:classes.zip

    Here . is the current directory and $JAVA is the main Java directory

    where the different tools like javac were installed.