0% found this document useful (0 votes)
3 views4 pages

Java Package

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined types. The document provides examples of creating and using packages, including syntax for compiling and importing classes. It also lists several built-in packages and demonstrates how to implement and call methods from classes within those packages.

Uploaded by

latestcricket143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java Package

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined types. The document provides examples of creating and using packages, including syntax for compiling and importing classes. It also lists several built-in packages and demonstrates how to implement and call methods from classes within those packages.

Uploaded by

latestcricket143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Package

 A java package is a group of similar types of classes, interfaces and sub-packages.


 Package in java can be categorized in two form, built-in package and user-defined
package.
 There are many built-in packages such as java, lang, awt, javax, swing, net, io, util,
sql etc.

The package keyword is used to create a package in java.


//save as [Link]
package letmecalculate;

public class Calculator {


public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
[Link]([Link](10, 20));
}
}

Now lets see how to use this package in another program.


import [Link];
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
[Link]([Link](100, 200));
}
}

How to compile java package:


javac -d directory javafilename

For example
javac -d . [Link]

To Compile: javac -d . [Link]


To Run: java [Link]

Import java io.*


Import [Link];
Base Package – Java
Sub Package – util
Classes- Scanner
Import [Link].*
Import [Link].*;
Import [Link];

Java.

util

Scanner()

NextInt()

NextFloat()
[Link].*

Buffered Reader()

Read()

ReadLine()

SYNTAX:

Package Package-Name;

Pre defined:

1. [Link]

2. [Link]

3. [Link]

4. [Link]

5. [Link]

Example:
package pack;
public class PackDemo
{
public void show()
{
[Link]("WELCOME TO JAVA");
}}
Importing package and class:
import [Link];
class Pack1
{
public static void main(String args[])
{
PackDemo obj1=new PackDemo();
[Link]();
}}
Importing another class with same package:
package pack;
public class Factorial
{
public void Fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f*=i;
}
[Link]("Factorial of " + n + " is " + f);
}}
Importing package and class:
import [Link];
import [Link];
public class FactorialMain
{
public static void main(String args[])
{
Factorial obj1=new Factorial();
[Link](5);
PackDemo obj=new PackDemo();
[Link]();
}}

You might also like