A package in Java is a grouping of related classes, interfaces, and sub-packages. Packages allow classes to be organized and avoid naming collisions. There are built-in packages provided by Java and user-defined packages. Some key built-in packages include java.lang, java.util, java.io, java.awt, and javax.swing. Packages provide advantages such as grouping related code, organizing large projects, and controlling visibility and access to classes.
A package in Java is a grouping of related classes, interfaces, and sub-packages. Packages allow classes to be organized and avoid naming collisions. There are built-in packages provided by Java and user-defined packages. Some key built-in packages include java.lang, java.util, java.io, java.awt, and javax.swing. Packages provide advantages such as grouping related code, organizing large projects, and controlling visibility and access to classes.
A java package is a group of similar types of classes, interfaces and sub-
packages. Package allows us to create a class name list which can be stored in on our own package and it is ensured that if we create one or more classes in other package with a same name they can not collides on each other. Packages basically contains for classes to keep class namespace compartmentalized. In short package is collection of related classes and interfaces stored together . Package in java can be categorized in two form, 1.built-in package 2. user-defined package. Some Built in package (API Package)
➢ java.lang - import java.lang.* ;
➢ java. util - import java.util .*; ➢ java.io - import java.io.*; ➢ java.applet - import java .applet. *; ➢ java.awt - import java.awt .*; ➢ javax.swing-import javax.swing.*; ➢ java.net –import java.net.*; ➢ java.sql -import java.sql.*; 1) java.lang : This is a language support package which is imported automatically by java compiler itself. 2) util package: This package are the language utility classes support such as linked list, stack, vector, date etc. 3) io package : This are the input output support package this package also support for file stream class. 4) applet package : This package is use for graphical application such as drawing different types of shape, creating font etc 5)awt package : Abstract Window Tool Kit :This package is used for creating GUI application (Graphical user Interface) 6) javax.swing It is same as awt package but it contains some extra classes such as dialogue box, table, tree etc. 7)java.net This package gives support for networking classes 8) java.sql This package support for database application . User Defined package : To create user defined package keyword package is use Synatx: package package name; //save by A.java //save by B.java package pack; import pack.*; public class A class B { { public void msg() public static void main(String args[]) { { System.out.println("Hello"); A obj = new A(); } obj.msg(); } } } Demo Code: Package pack.subpack ; Package pack ; public class class2 public class class1 { { public void farewell() public void greet () { { System.out.println(" Good Bye"); System.out.println(" Welcome "); } } } } ty pack subpack class2.java ty pack(folder) class1. java Create a subdirectory called subpack, javac class1.java open a new file and enter the above code this directory must be kept in pack create a directory called pack open a new directory the above 2 package are only file & enter the above code save this file complied do not run this package as class1. java in pack directory import pack. * ; import pack. subpack.*; class test30 { public static void main (String args []) { class1 t= new class1 (); t.greet () class 2 t1 = new class 2(); t1. farewell; } } ty → test30. java Save the above code in root directory , compile & execute the above code which gives the output of both the package Create the package called Home pack. Create 2 classes expenditure and income in it. Expenditure class consist a variable to store food, clothing & educational expenses. Income class has a variable to store salary, rent, allowances. Create class called budget which calculate the savings of family package homepack; import homepack.*; public class exp class budget { { public int f = 2000; public static void main(String args[]) public int c= 1500; { public int e = 5000; exp t = new exp(); } int tot = t.f+t.c+t.e; package homepack; System.out.println("total exp" +tot); public class income income t1=new income( ) { int tot2 =t1.sal +t1rent +t1.all; public int sal = 10000; System.out.println("total income”+tot2) public int re = 2000; int sav= tot2- tot1; public int all = 1000; System.out.println ("total Saving" + sav); } } } Create a package named maths and stat. Create two classes mathematics in package math and statistics in package stat. Write a method maximum and minimum in class mathematics which calculates maximum & minimum of three integers. Write a method average & median in statistics which calculate average & median of 3 integers Accept 3 integers from command line argument package math ; public class math1 package stat; public class stat1 { { public void max(int a, int b, intc) public void avg (int x, int y, int z) { { if (a>b && a>c) float avgl= (float ) x+y+z/3 ; System.out.println ("a is greater “ +a); System.out.println (" Avg of 3 int “+avg1); else } if (b>a && b>c) public void median (int x, int y, int z) System.out.println (" b is greater " +b); { int m[] = {x,y,z}; else for (int i=0; i<3 ; i++) System.out.println(" c is greater "+c); { } for(int j =i+l; j<3 ; j++) public void min(int a, int b, int c) { { if (m[i]>m[j]) if (a<b && a<c) { System.out.println ("a is minimum “ +a); int t=m[i]; else m[i] = m[j]; m[j] =t; if (b<a && b<c) }}} System.out.println (" b is minimum " +b); System.out.println(" median is " +m[1]); else } System.out.println(" c is minimum "+c); } } import math .*; import stat.*; class test 31 { public static void main( String args[]) { int x = Integer.parse Int(args [0]); int y = Integer.parseInt (args [1]); int z = Integer.parseInt (args[2]);
math1 t = new math1();
t.max (x,y,z); t.min (x, y, z); stat1 t1 = new stat1(); t1.avg (x,y,z); t1.median (x,y,z); } } Create a package name college which contains a class teacher with variable name and degree Teacher has 2 methods accept and display. Write a main function which access this teacher class import college.*; package college; class test 32 public class teacher { { public static void main(String args[]) public String nm, deg; { public void accept ( String name,String degree) String nm= args [0]; { String deg = args[1]; nm = name; teacher t = new teacher(); deg = degree, t.accept (nm, deg ); } t.display(); public void display () } { } System.out.println(" Name of Teacher " +nm); System.out.println(" Degree of Teacher" + deg); } } Advantages of Packages 1.Related classes and interfaces can be grouped together. 2. Packages help in organizing code. 3. Keeps the class name space compartmentalized i.e. avoids name collision. 4.Packages provide a visibility-control mechanism i.e. users can control access to classes and interfaces.