You are on page 1of 9
1 is necessary in software development Ie: - classes and interfaces, it is better if relationship. Thus, the classes and interface same directory. This directory or folder is also c a lackage aS i A package represents a directory that contains related group of classes and when we write: statements like: o , r vi a rs) ede import java.io." package. Here, java is a directory name and io is We are importing classes of java.io fall the classes and interfaces of that io st cirectory within it, And the “? represents Please look at the Figure 20.1. ee aaa u pe Figure 20.1 Package is a directory Scanned with CamScanner 238 | Chapter 20 A programmer is writing the following statements in a program: 1, ixiport java.awt.*; 2 import java.awt-event.*; ‘Should he write both the statements in his program or the first one is enough? event iS a sub package of java. awt package. But, when a package is imported, it ‘are not automatically imported into a program. So, for every package or sub packs import statement should be written. Hence if the programmer wants the classes and the java.awt and java.awt.event packages, then he should write both the p in his program. ‘There are several following advantages of package concept: Q Packages are usefull to arrange related classes and interfaces into a group. This classes and interfaces performing the same task to put together in the same. example, in Java, all the classes and interfaces which perform input and output stored in java. io package. Packages hide the classes and interfaces in a separate sub directory, so that accid of classes and interfaces will not take place. ‘ona . The classes and interfaces of a package are isolated from the classes and inte Qa package. This means that we can use same names for classes of two different example, there is a Date class in java.util package and also there is anoi available in java. sql package. an A group of packages is called a library. The classes and interfaces of a package are lie beols= a library and can be reused several times. This reusability nature of packages malts programming casy. Just think, the packages in Java are created by JavaSoft people only ox and millions of programmers all over the world are daily by using them in various programs. Different Types of Packages There are two different types of packages in Java. They are: H G Built-in packages Q User-defined packages Built-in Packages ‘These are the packages which are already available in Java language. These packages provide ® most all necessary classes, interfaces and methods for the programmer to perform any task in he programs. Since, Java has an extensive library of packages, a programmer need not think abo! logic for doing any task, For everything, there is a method available in Java and that method caf used by the programmer without developing the logic on his own. This makes the program easy. Here, we introduce some of the important packages of Java SE: - Q java.Jang: 1ang stands for language. This package got primary classes and interfeee essential for developing a basic Java program. It consists of wrapper classes which are vy convert primitive data types into objects. There are classes like String, StringBuffer to p strings. There is a Thread class to create various individual processes. Runtime and SiS" lasses are also present in java.lang package which contain methods to execute ® application and find the total memory and free memory available in JVM Scanned with CamScanner 'gc() method. This gc() method appears in both Runtime and System classes 5 package. For example, we can call it as: 5 11 stands for utility. This package contains useful classes and interfaces ke Hashtable, Vector, Arrays, etc. These classes are called collections. There are handling date and time operations. for input and output. This package contains streams. A stream represents ‘one place to another place. Streams are useful to store data in the form of Sles input-output related tasks. for abstract window toolkit. This package helps to develop GUT ec) where programs with colorful screens, paintings and images etc. ca sists of an important sub package, java which is useful to ‘components like push buttons, radio buttons, menus etc. “ package helps to develop GUI like java.awt. The X' in Savex represents d which means it is a package developed from another package by fit In fact, javax. swing is an extended package of 3ava.a"=- for network. Client-Server programming can be done by using this to obtaining authentication for a network, creating sockets at cHent ‘communication between them are also available in 32 package. from a server into a client and get executed ‘are programs which come is useful to create and use na network. Applet class of this package = has two important classes, Dat to format dates and times, ‘useful to format numeric values. Structured query language. This package helps to connect fo etrieve the data from them and use it in a Java program earlier, the users of the Java language can also create theif packages. User-defined packages can also Pe imported fin the same way as the Built-in packages ‘own and use it in any other class. To create a package the i Scanned with CamScanner Ba ee Be ee OE me re ores tite he “a package with the name peck and store Addition Class ee Scanned with CamScanner eaarRNEs RL a 28) eae PEAR Saatetonct0, 15.595 - sum) tho O} “a class of a package, we should write the package name before the claws {on in the preceding program. This is inconvenient for the programmer, To import statement only once in the beginning of the program, # the package name before written as shown earlier, we need not to use t to Addition class, in a “of the program and we can create the object je using the import statement to import & package and Its ‘a clans Addition to it, Similarly, we od which performs aubtraction of {wo numbers, medture should be repeated, First of all, write the eearin Progtam 4, And then write subtraction ypack and ald bE Scanned with CamScanner ‘Pack is already existing 1 = already Addition Scanned with CamScanner tion class, Purpose, the user takes the contains the description of al d later in this chapter 4, we are using multiple import statements as: classes of the same package, we need n f 3 ot writs te import statement write a single import statement as: Perce Fe ae the following two statements: Addition class of the package pack is imported into the program and in and interfaces of the package pack are available to the program. fo import only one class of a package say Java compiler links up the BufferedReader of 5a mites import statement as: E compiler conducts a search for BufferedReader class in java package, ‘the rest of the program. This increases load on the compiler and hence However, there will not be any change in the runtime » Here, rt statement to represent alll papece bere Here, we.are using import stat 0 represent alll the lat any of the Addition. java and Subt ace an rectory. Delete them from the current directory as they cause confusion ‘compiler looks for byte code in addition. java and Sabrrace ble there and hence it lags some errors. use import statement in a different way. Remove Ac m current directory and then test this program. Scanned with CamScanner Scanned with CamScanner Fee) ea ee meee sk eM tie ass tieeecne a oewtaS sc se Teme oe een os Se Se en Se oe Se Senda oe Te a Sa atest Sa ee (jee eee met Se SS as We eS ae mr om = So a fet Scanned with CamScanner

You might also like