Eight packages comprise the standard Java development environment.
The Java Language Package The Java language package, also known as java.lang, contains classes that are core to the Java language. The classes in this package are grouped as follows: Object The granddaddy of all classes--the class from which all others inherit. Data Type rappers ! collection of classes used to wrap varia"les of a primitive data type: #oolean, $haracter, and Dou"le, %loat &nteger and 'ong. Strings Two classes that implement character data. The (tring and (tring #uffer $lasses is a thorough lesson on the use of "oth types of strings. System and Runtime These two classes provide let your programs use system resources. (ystem provides a system-independent programming interface to system resources and )untime gives you direct system-specific access to the runtime environment. *sing (ystem )esources Descri"es "oth the (ystem and )untime classes and their methods. Threads The Thread, Thread Death and Thread +roup classes supplement the multi-threading capa"ilities so important to the Java language. The java.lang package also defines the runna"le interface. )unna"le makes it convenient for Java class to "e active without su"classing the Thread class. Through an e,ample- oriented approach Threads of $ontrol will teach you a"out Java threads. Classes The $lass provides a runtime description of a class and the $lass 'oader class allows you to load classes into your program during runtime. Math The -ath class provides a li"rary of math routines and values such as pi. hen an error occurs in a Java program, the program throws an o"ject, which indicates what the pro"lem was and the state of the interpreter when the error occurred. .nly o"jects that derive from the throwa"le class can "e thrown.There are two main su"classes of Throwa"le: E,ception and Error. E,ceptions are a form of Throwa"le that /normal/ programs may try to catch. Errors are used for more catastrophic errors-- normal programs should not catch errors. The java.lang package contains the Throwa"le, E,ception, and Error classes, and numerous (u"classes of E,ception and Error that represent specific 0ro"lems. 1andling errors using e,ceptions shows you how to use e,ceptions in your Java programs to handle errors. Process 0rocess o"jects represent the system process that is created when you use )untime to e,ecute system commands. The java.lang 0ackage defines and implements the generic 0rocess class. The compiler automatically imports this package for you. 2o other packages are automatically imported. The Java IO Package The Java &3. 0ackage 4java.io5 provides a set of input and .utput streams used to read and write data to files or other&nput and output sources. The classes and interfaces defined&n java.io are covered fully in &nput and .utput (treams. The Java !tility Package This Java package, java.util, contains a collection of utility classes. !mong them are several generic data structures 4Dictionary, (tack, 6ector, and 1ash ta"le5 a useful o"ject for tokeni7ing a string and another for manipulating calendar dates. The java.util package also contains the ."server interface and ."serva"le class, which allow o"jects to notify one another when they change. The java.util classes aren8t covered separately in this tutorial although some e,amples use these classes. The Java "et#orking Package The java.net package contains classes and interface definitions that implement various networking capa"ilities. The $lasses in this package include a class that implement a *)', a connection to a *)', a socket connection, and a datagram packet. 9ou can use these classes to implement client-server applications and other networking communication applications.$ustom 2etworking and (ecurity has several e,amples using these classes, including a client-server e,ample and an e,ample that uses datagrams. The $%%let Package This package contains the !pplet class, the class that you must su"class if you8re writing an applet. &ncluded in this 0ackage is the !udio $lip interface which provides a very high level a"straction of audio. riting !pplets e,plains the ins and outs of developing your own applets. The $bstract &indo# Toolkit Packages Three packages comprise the !"stract indow Toolkit: Java.awt, java.awt.image, and java.awt.peer. $&T Package The java.awt package provides graphical user interface 4+*&5 elements that are used to get input from and display information to the user. These elements include windows, "uttons, scroll"ars, and te,t items. $&T Image Package The java.awt.image package contains classes and interfaces for managing image data, such as setting the color model, cropping, color filtering, setting pi,el values, and gra""ing snapshots of the screen. $&T Peer Package The java.awt.peer package contains classes and interfaces that connect platform-independent !T components to their platform-dependent implementation 4such as -otif widgets or -icrosoft indows controls5. $reating a *ser &nterface covers all three of the !T packages. '($T!R(S O' J$)$ *istributed Java has an e,tensive li"rary of routines for coping with T$03&0 protocols like 1TT0 and %T0 Java applications can open and access across the 2et via *)'s with the same ease as when accessing local file system.e have found the networking capa"ilities of Java to "e "oth strong and easy to use. !nyone who has tries to do &nternet programming using another language will revel. 1ow simple Java makes onerous tasks will like opening a socket connection. Robust Java is intended for writing programs that must "e reada"le in a 6ariety ways. Java puts a lot of emphasis on early checking for possi"le pro"lems, later dynamic checking, and eliminating situations that are error prone. The single "iggest difference "etween Java has a pointer model that eliminates the possi"ility of overwriting memory and corrupting data. The Java compiler detects many pro"lems that in other languages would only show up at runtime. !s for the second point, anyone who has spent hours chasing a memory leak cost "y a printer "ug will "e very happy with this feature of Java. Java gives you the "est of "oth worlds. 9ou need not pointers for everyday constructs like string and arrays. 9ou have the power of pointers if you need it, for e,ample, for like lists. !nd you have always-complete safety, since you can never access a "ad pointer or make memory allocation errors. Secure Java is intended to "e used in networked3distri"uted environment toward that end: a lot of emphasis has "een placed on security. Java ena"les the contraction of virus-free, temper- free systems. 1ere is a sample of what Java;s security features are supposed to keep a Java programming from doing: <. .verrunning the runtime stack. =. $orrupting memory outside its own process space. >. )eading or writing local files when invoked through a security-conscious class loader like e" "rowser. $rchitecture "eutral The compiler generates an architecture neutral o"ject file format- the compiled code is e,ecuta"le on many processors, given the presence of Java runtime system...The Java compiler does this "y generating "yte code instructions which have nothing to do with a particular computer architecture. )ather they ere designed to "e "oth easy to any machine and easily translated into native machine code on the fly. Twenty years ago, the *$(D 0ascal system did the same thing in a commercial product and, even "efore that, 2icholas orth;s original implementation of 0ascal used the same approach. #y using "ytecodes, performance takes major hit. The designers of Java did an e,cellent jo" developing a "yte code instruction set those workers well on today;s most common computer architectures. !nd the codes have "een designed to translate easily into actual machine instructions. Portable *nlike $ and $??, they are no /implementation dependent/ aspects of the specifications. The si7es of the primitive;s data types are specified, as is the "ehavior of arithmetic on them. %or e,ample, an int in Java is always a >=-"it integer. &n $3$??, int can mean a <@-"it integer, a >=-"it integer, or any si7e the compiler vendor likes. The only restriction is that it must have at least as many "ytes int and cannot have more "ytes than a long int. The li"raries that are a part of the system define porta"le interfaces. %or e,ample, there is an a"stract window class and implementations of it *2&A, indows, and the -acintosh. Inter%reted The Java interpreters can e,ecute Java "yte codes directly on any machine to which the interpreter has "een ported. (ince linking is a more incremental and lightweight process, the development process can "e much more rapid and e,planatory. .ne pro"lem is that the JDB is fairly slow at compiling your source code to the "ytecodes that will, ultimately, "e interpreted in the current version. +igh Per,ormance hile the performance of interpreted "ytecodes is usually more than adeCuate, there are situations higher performance is reCuired. The "ytecodes can "e translated on fly into machine code for the particular $0* the application is running on. 2ative code compilers for Java are not yet generally availa"le. &nstead there are just-in-time 4jit5 compilers. These work "y compiling the "yte codes into native code once, caching the results, and then calling them again, if needed. This speeds up code once, catching the results, and calling them again, if needed. This speed up the loop tremendously since once has to do the interpretation only once. !lthough still slightly slower than a true native code compiler, just-in-time compilers can give you a <D-or even =D-fold speedup for some programs and will almost always "e significantly faster than the Java &nterpreter. Multithreaded &n a num"er of ways, Java is more dynamic language than $ or $??. &t was designed to adapt to an evolving environment. 'i"raries can freely add new methods and instance varia"les without any effect on their clients.... &n Java, finding out run time type information is straightforward. This is an important feature in those situations where code needs to "e added to a running program. ! prime e,ample is code that is downloaded from the &nternet to run in "rowser. P$R$*I-M O' J$)$ Java as a programming language evolved keeping in view certain criteria and features. &t was "uilt around a model that had these features. (ome of the features of this model we have seen like the o"ject oriented, architecture neutral, ro"ust, secure etc. (ome additional features are e,plaining "elow Dynamic downloading of applets- !pplets should "e downloada"le on to a client machine as and when reCuired. Elimination of fatter phenomenon - Java "ased products could eliminate these "y giving the users only those features of a product that the user needs at a time. The remaining features of a product can remain on the server itself. (upports 2etworks centric computing- Java should "e a"le to support low cost 2etwork computers.
(upports $.)#!E D$.--Java supports $ommon ."ject )eCuest #roken !rchitecture 4$.)#!5 and Distri"uted $ommon ."ject -odel 4D$.-5. This ensures that certain standards for o"ject 0rogramming are net. (upports !ctive A also. J$)$ $PPL(TS !pplets are a common way of writing Java applications. &ntegrating we" "ased sound and graphics into applications is simplified "y using methods in !pplet class. !pplets are essentially program that run from within a "rowser. They appear as part of 1T-' documents in the same way that pictures are presented. The major difference is that instead of a static graphics, the Java !pplet is a complete running program. !n !pplet is just like a widow application, "ut without a classic window frame. The range of programs that can "e written as an !00'ET are reduced "ecause of security limitations imposed "y the target "rowser. !pplets run only from with in Java-Ena"led "rowsers such as 2ET($!0E, hotJava, and &2TE)2ET EA0'.(E). $*)$"T$-(S O' $PPL(TS There are several advantages using !pplets. The most o"vious is that you only need one copy of your production class files on an 1T-' server. This reduces the nightmare of distri"uting and installing software "y the tape or disk. &nternet and &ntranet "ased software also reduces the pro"lems of '!2 accesses to software instead of accessing a disk "y way of a network-mounted disk. %iles are accessed through http and %T0. E# pages can also "e used as a method of presenting help to your users as well as keeping them informed a"out the latest changes to your programs. This ena"les due to keep in constant touch your users. *IS$*)$"T$-(S O' $PPL(T )unning your application from a we" "rowser is not necessarily a good thing. Target users must "e running a version of we" "rowser that supports that Java. &t also helps if uses have an &nternet connection of =F.F k or higher on relatively fast computers. !nother large pro"lem is local file access. &f the applet was loaded from an http server, no local files can "e read or written to on the client machines. 'oading the applet from the users local disk storage can alleviate this, "ut this defeats any of the we"-"ased applet advantages mention earlier. !pplets have reduced network access. &f an applet is loaded from networked machine. .nly that machine can "e communicated with a via a socket connection this prevents the applet from communicating to other machines on we". This security restriction is alleviated if the applet is loaded from the users local disk. !ny Java "ased graphical application can "e easily converted into an applet. This id "ecause "oth frame and applet are e,ecuted from the container class. $PPL(T LI'( C.CL( There are four methods that give the framework to "uild the applet &nit 45 (tart 45 (top 45 Destroy 45 &nit45:- *sed for initial setup such as defining 'ayout, passing parameters and other initiali7ations. (tart45:- This is called immediately after init 45 start 45 is called each time user return to the pea or when the page is deconified. *nlike start, init is called only once. (top45:- This method is called when the user moves off the page on which the !pplet sits or when it is iconified. &f an applet if not doing time consuming activities like performing animation, this need not "e implemented. Destroy45:- This method is called when the "rowser shuts down. &t is used to re-claim resources. J$)$ )S C// The synta, of Java looks very much like $??. $ompared to c??, Java used similar synta, for if and loop constructs. The notion of a constructor is all very similar to what $?? has. The following are some of the differences of Java over $??. &f you understand these differences you will see why Java is such a "eneficial programming language. $ompare to $?? Java runs =D times slower "ecause of platform independents. Every thing must "e in a class. There are no glo"al functions or glo"al data. &f you want the eCuivalent of glo"al, make static methods and static data within a class. There are no structures or enumeration or unions. .nly class. The $har type uses the international <@-"it. *nicode character set, so it can automatically represent most national characters. !ll non-primitive types can only "e created using new. !ll primitive types can only "e created directly, without new. There are wrapper classes for all primitive classes so you can create eCuivalent heap-"ased o"jects with new Java has no 0reprocessors. &f you want to use classes in another li"rary, you say import and the name of the li"rary. There are no preprocessor-like macros. There are no Java pointers in the sense of $ and $??. hen you create an o"ject with new, you get "ack a reference. %or e,ample (tring s G new (tring 4/peers/5: Java has no templates or other implementation of parameteri7ed types. There is a set of containers : 6ectors, stacks and 1ash ta"le that hold o"ject references, and through which you can satisfy your container needs, "ut these containers are not designed for efficiently like the $?? standard template li"rary4(T'5 Java has "uilt-in support for comment document action, so the source code file can also contain its own documentation, which is stripped out and reformatted into 1T-' using an apart program. This is "oon for documentation maintenance and use. Java has standard li"raries for solving specific tasks. $?? relies on non-standard third-party li"raries. These tasks include - 2etworking - Data"ase $onnection 4via JD#$5 - -ultithreading - Distri"uted ."jects 4via )-& and $.)#!5 - $ompression - $ommerce The availa"ility and standard nature of these li"raries allow for more rapid application development. +enerally, Java is more ro"ust ."ject handles initiali7ed to null 1andles are always checked and e,ceptions are thrown for failure all array accesses are checked for "ounds violations !utomatic gar"age collection prevents memory leaks $lean, relatively fool-proof e,ception handling "ytecodes verification of network applets