0% found this document useful (0 votes)
7 views10 pages

Unit 3.2-Packages

Java

Uploaded by

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

Unit 3.2-Packages

Java

Uploaded by

Aryan Gireesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Understanding

Java Packages
Table of Contents

► 1. Packages Concept
► 2. Creating User-Defined Packages
► 3. Java Built-in Packages
► 4. Import Statement and Static Import
Packages Concept

► Definition: A package is a namespace that organizes a set of


related classes and interfaces.
► Purpose: Helps in avoiding name conflicts, and controls
access.
► Types:
► Built-in packages (provided by Java API)
► User-defined packages (created by developers)
► Syntax:
► java
► package packageName;
Advantages of Using Packages

► Namespace Management: Prevents naming conflicts by


grouping related classes.
► Access Protection: Defines visibility rules for classes and
interfaces.
► Reusability: Encourages code reuse by packaging related
functionality together.
► Maintenance: Easier to manage and update the code.
Creating User-Defined
Packages
► Steps to Create a Package:
► 1. Declare the package at the beginning of the Java file.
► 2. Define the class/interface within the declared package.
► 3. Compile the classes and store them in appropriate directories.
► Example:
► package mypackage;
► public class MyClass {
► public void display() {
► System.out.println("Welcome to My Package");
► }
► }
► Directory Structure:
► - mypackage/MyClass.java
Using User-Defined Packages

► Compile the package:


► shell
► javac MyClass.java
► Access the package:
► import mypackage.MyClass;
► public class Test {
► public static void main(String[] args) {
► MyClass obj = new MyClass();
► obj.display();
► }
► }
Java Built-in Packages
► Common Java Packages:
► - java.lang (default package, no import needed)
► - java.util (contains utility classes like ArrayList, HashMap)
► - java.io (for input-output operations)
► - java.net (for networking)
► - java.awt (for GUI components)
► Example:
► java
► import java.util.ArrayList;
► public class Example {
► public static void main(String[] args) {
► ArrayList<String> list = new ArrayList<>();
► list.add("Hello");
► list.add("World");
► System.out.println(list);
► }
► }
Import Statement
► Purpose: To bring one or more classes or entire packages into visibility.
► Types of Import Statements:
► Single type import
► java
► import java.util.List;
► On-demand type import
► import java.util.*;
► Example:
► import java.util.Date;
► public class DateExample {
► public static void main(String[] args) {
► Date date = new Date();
► System.out.println(date);
► }
► }
Static Import
► Definition: Allows the use of static members of a class without
specifying the class name.
► Syntax:
► java
► import static java.lang.Math.*;
► Example:
► import static java.lang.Math.PI;
► import static java.lang.Math.sqrt;
► public class StaticImportExample {
► public static void main(String[] args) {
► double radius = 5.0;
► double area = PI * radius * radius;
► double root = sqrt(area);
► System.out.println("Area: " + area);
► System.out.println("Square Root: " + root);
► }
► }
Summary

► Key Points Covered:


► - Packages concept and benefits
► - Steps to create user-defined packages
► - Common Java built-in packages
► - Import statement usage and static import

You might also like