0% found this document useful (0 votes)
28 views

Package in Java

A package organizes Java classes and interfaces into logical groups. There are predefined Java packages like java.lang and user-defined packages. Packages provide advantages like reusability, security, and avoiding naming conflicts. To use a class from another package, it must be imported. The document provides examples of using predefined and user-defined packages in Java code.

Uploaded by

yuvikatuteja4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Package in Java

A package organizes Java classes and interfaces into logical groups. There are predefined Java packages like java.lang and user-defined packages. Packages provide advantages like reusability, security, and avoiding naming conflicts. To use a class from another package, it must be imported. The document provides examples of using predefined and user-defined packages in Java code.

Uploaded by

yuvikatuteja4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PACKAGE IN JAVA

A package arrange number of classes, interfaces, and sub package of same type in particular

Group.

Package is nothing but folder in windows.

TYPES OF PACKAGE

Pre- defined packages User- defined packages

Java.lang any name pi, mypack, etc..

Java.util

Java.io

Java.net

Java.applet

Java.awt

Access modifier in package

Access modifier Within package Outside package by Outside package


sub class
private no no no
default yes no no
protected yes yes no
public yes yes yes

Advantages of package

1- Reusability
2- Security
3- Fast searching
4- Naming conflicting
5- Hiding

Disadvantages of package

1- Can’t pass parameters to package

Pre-defined packages

1- java.lang: default package, heart of java because without this package we can’t write
even single program, no need to import this package.
System, String, Object, Integer etc. classes in this package
2- java.util: used to implement data structure of java, contain utility classes, also known as
collection framework.
Linklist, Stack, Vector, Hashset, Treeset, etc…

3- java.io: very useful to perform input output operations of file.


File, FlieWriter, FileReader classes of this package.

4- java.applet: mainly use to develop GUI application, applet programs are web related
program created at server but executed at client machine.
Applet class of this package.
5- java.awt: awt stands for abstract window toolkit, used to develop GUI applications,
It is standalone program, it contain main() unlike applet.
Frame, Button, TextField
6- java.net: related with networking, used to developed the client server applications.
URL, InetAddress, URLconnection etc…
7- java.sql: use for database connectivity.
Connection, Statement, Resultset etc

Example of pre-defined package

import java.util.Scanner;

class MyClass {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Enter username");

String userName = myObj.nextLine();

System.out.println("Username is: " + userName);

User-defined package
package myFirstPackage;
class Main {
public static void main(String args[]) {
System.out.println("Wooohooo!! I created my first package");
}
}
For compile
javac -d . Main.java

for run
java myFirstPackage.Main

Another example of package


package MyPackage;public class Compare {
int num1, num2;Compare(int n, int m) {
num1 = n;
num2 = m;
}public void getmax(){
if ( num1 > num2 ) {
System.out.println("Maximum value of two numbers is " + num1);
}else {
System.out.println("Maximum value of two numbers is " + num2);
}
}public static void main(String args[]) {
Compare current[] = new Compare[3];
current[1] = new Compare(5, 10);
current[2] = new Compare(123, 120);
for(int i=1; i < 3 ; i++)
{
current[i].getmax();
}
}
}

Importing a package
package Edureka;
import MyPackage.Compare;public class Demo{
public static void main(String args[]) {
int n=10, m=10;
Compare current = new Compare(n, m);
if(n != m) {
current.getmax();
}
else {
System.out.println("Both the values are same");
}
}
}

You might also like