SlideShare a Scribd company logo
1 of 9
• A Package can be defined as a grouping of related
types(classes, interfaces)
• A package represents a directory that contains related group of
classes and interfaces.
• Packages are used in Java in order to prevent naming conflicts.
• There are two types of packages in Java.
1. Pre-defined Packages(built-in)
2. User defined packages
import java.util.Scanner;
Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.
Advantages of using a package in Java
Reusability:
Better Organization:
Pre-defined Packages
Package Name Description
java.lang Contains language support classes (for e.g classes which defines
primitive data types, math operations, etc.). This package is
automatically imported.
java.io Contains classes for supporting input / output operations.
java.util Contains utility classes which implement data structures like Linked
List, Hash Table, Dictionary, etc and support for Date / Time
operations. This package is also called as Collections.
java.applet Contains classes for creating Applets
java.awt Contains classes for implementing the components of graphical user
interface ( like buttons, menus, etc. ).
java.net Contains classes for supporting networking operations
java.sql This package helps to connect to databases like
Oracle/Sybase/Microsoft Access to perform different operations.
Defining a Package(User defined):
To create a package is quite easy: simply include a package command as the first
statement in a Java source file.
Any classes declared within that file will belong to the specified package.
The package statement defines a name space in which classes are stored.
If you omit the package statement, the class names are put into the default package,
which has no name.
This is the general form/syntax of the package statement:
package pkg;
Here, pkg is the name of the package.
package calculation;
public class Calculator
{
public int add(int a, int b)
{
return a+b;
}
public static void main(String args[])
{
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}
Compilation for package program
//this will creates package in your directory
•javac -d . Filename.java
//compile and stores class file in your package
•javac –d .. Filename.java
// this is running command
•java packagename.Filename
Demonstrate user defined package by the help of java programs
import calculation.Calculator;
public class Demo
{
public static void main(String args[])
{
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}
package import
Java swing: Swing is an Extension library to the AWT.
Difference between AWT and Swing
Java AWT Java Swing
AWT components
are platform-dependent.
Java swing components
are platform-independent.
AWT components
are heavyweight.
Swing components
are lightweight.
AWT doesn't support
pluggable look and feel.
Swing supports pluggable look
and feel.
AWT provides less
components than Swing.
Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser,
tabbedpane etc.
AWT doesn't follows MVC Swing follows MVC.
Swing Components
import javax.swing.*;
public class FirstSwingExample
{
public static void main(String[] args)
{
//creating instance of JFrame
JFrame f=new JFrame();
//creating instance of JButton
JButton b=new JButton("click");
//x axis, y axis, width, height
b.setBounds(130,100,100, 40);
//adding button in JFrame
f.add(b);
//400 width and 500 height
f.setSize(400,500);
//using no layout managers
f.setLayout(null);
//making the frame visible
f.setVisible(true);
}
}

More Related Content

Similar to Java Packages and Package Structure

Similar to Java Packages and Package Structure (20)

Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Java packages
Java packagesJava packages
Java packages
 
Java packags
Java packagsJava packags
Java packags
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 
Packages in java
Packages in javaPackages in java
Packages in java
 

More from VeenaNaik23

Introduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSIntroduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSVeenaNaik23
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptxVeenaNaik23
 
HIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxHIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxVeenaNaik23
 

More from VeenaNaik23 (10)

Introduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSIntroduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFS
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
 
wcmwiki.pptx
wcmwiki.pptxwcmwiki.pptx
wcmwiki.pptx
 
web1.pdf
web1.pdfweb1.pdf
web1.pdf
 
canvas.pptx
canvas.pptxcanvas.pptx
canvas.pptx
 
HIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxHIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptx
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
Mis
MisMis
Mis
 
unit3.3.pptx
unit3.3.pptxunit3.3.pptx
unit3.3.pptx
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 

Recently uploaded

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

Java Packages and Package Structure

  • 1. • A Package can be defined as a grouping of related types(classes, interfaces) • A package represents a directory that contains related group of classes and interfaces. • Packages are used in Java in order to prevent naming conflicts. • There are two types of packages in Java. 1. Pre-defined Packages(built-in) 2. User defined packages
  • 2. import java.util.Scanner; Here: → java is a top level package → util is a sub package → and Scanner is a class which is present in the sub package util. Advantages of using a package in Java Reusability: Better Organization:
  • 3. Pre-defined Packages Package Name Description java.lang Contains language support classes (for e.g classes which defines primitive data types, math operations, etc.). This package is automatically imported. java.io Contains classes for supporting input / output operations. java.util Contains utility classes which implement data structures like Linked List, Hash Table, Dictionary, etc and support for Date / Time operations. This package is also called as Collections. java.applet Contains classes for creating Applets java.awt Contains classes for implementing the components of graphical user interface ( like buttons, menus, etc. ). java.net Contains classes for supporting networking operations java.sql This package helps to connect to databases like Oracle/Sybase/Microsoft Access to perform different operations.
  • 4. Defining a Package(User defined): To create a package is quite easy: simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name. This is the general form/syntax of the package statement: package pkg; Here, pkg is the name of the package.
  • 5. package calculation; public class Calculator { public int add(int a, int b) { return a+b; } public static void main(String args[]) { Calculator obj = new Calculator(); System.out.println(obj.add(10, 20)); } } Compilation for package program //this will creates package in your directory •javac -d . Filename.java //compile and stores class file in your package •javac –d .. Filename.java // this is running command •java packagename.Filename Demonstrate user defined package by the help of java programs
  • 6. import calculation.Calculator; public class Demo { public static void main(String args[]) { Calculator obj = new Calculator(); System.out.println(obj.add(100, 200)); } } package import
  • 7. Java swing: Swing is an Extension library to the AWT. Difference between AWT and Swing Java AWT Java Swing AWT components are platform-dependent. Java swing components are platform-independent. AWT components are heavyweight. Swing components are lightweight. AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel. AWT provides less components than Swing. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. AWT doesn't follows MVC Swing follows MVC.
  • 9. import javax.swing.*; public class FirstSwingExample { public static void main(String[] args) { //creating instance of JFrame JFrame f=new JFrame(); //creating instance of JButton JButton b=new JButton("click"); //x axis, y axis, width, height b.setBounds(130,100,100, 40); //adding button in JFrame f.add(b); //400 width and 500 height f.setSize(400,500); //using no layout managers f.setLayout(null); //making the frame visible f.setVisible(true); } }