15 Packages
15 Packages
Java API
API - Application Programming Interface.
Java API - Collection of packages.
Package
collection of classes, interface and sub
packages in the form of .class file.
Purpose of package:
To place common classes and interfaces.
(share for other java programmers/Developers).
Without package concepts
• Programmers possible to develop own
code (Specific requirement).
• Not possible to get common requirement.
• Other programmer code not possible
access.
OUTPUT
Enter your name
Balaji
Your name is Balaji
Purpose
To place common classes and interfaces (user defined).
eg:
package test.paper;
package language.english.teach;
here,
test,language - parent package
paper,english - sub-package.
teach - sub-sub-package
How to compile package
Syntax:
javac -d<space>.<space><file name>.java
here,
javac - java compiler
-d - directory
. - Current directory
. . - parent directory
eg:
javac -d . Lists.java
package detail;
public class List1
{
public static void main(String[] args)
{
System.out.println("Hello package");
}
}
compile package : javac -d . List1.java
Execute : java detail.List1
OUTPUT
Hello package
Sample program for user defined package (with out main method)
package menu; package finals;
public class Menu import menu.*;
{ import food.Food;
public int menuList(int add) public class Finals
{ {
return add; public static void main(String[] args)
} {
Menu m=new Menu();
}
int list=m.menuList(24);
save : Menu.java
Food f=new Food();
String name=f.foodDetail("POORI");
package food; System.out.println(name+" "+list);
public class Food }
{ }
public String foodDetail(String name)
{ save : Finals.java
return name; Compile
} javac -d . Menu.java
} javac -d . Food.java
save : Food.java javac -d . Finals.java
Execute java finals.Finals
Package example – commonly access by n number of
developer
package operation.add;
public class Addition
{
public int add(int[] adds)
{
int[] a=adds;
int sum=0;
int len=a.length;
for(int i=0;i<len;i++)
{
sum=sum+a[i];
}
return sum;
}
}
//save as :Addition.java
//compile as : javac -d . Addition.java
import operation.add.Addition;
import java.util.Scanner;
class Access
{
public static void main(String[] args)
{
Addition a=new Addition();
System.out.println("How many values to enter");
Scanner s=new Scanner(System.in);
int size=s.nextInt();
int[] data=new int[size];
System.out.println("start values to enter");
for(int i=0;i<size;i++)
{
data[i]=s.nextInt();
}
int sumOfArray=a.add(data);
System.out.println("sum of array is "+sumOfArray);
}
} //save as : Access.java
//compile as : javac Access.java
//execute as : java Access