0% found this document useful (0 votes)
5 views3 pages

Lecture 7 - Java Library Packages

Java packages are collections of related classes and interfaces that help organize and manage code, avoid naming conflicts, and enhance reusability. The Java Standard Library includes various packages, such as java.util for collections and java.io for input/output operations, which are structured hierarchically. To use classes from these packages, they must be imported into the Java file using the 'import' statement.

Uploaded by

nankhunibrian
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)
5 views3 pages

Lecture 7 - Java Library Packages

Java packages are collections of related classes and interfaces that help organize and manage code, avoid naming conflicts, and enhance reusability. The Java Standard Library includes various packages, such as java.util for collections and java.io for input/output operations, which are structured hierarchically. To use classes from these packages, they must be imported into the Java file using the 'import' statement.

Uploaded by

nankhunibrian
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

Java library packages

● In Java, a package is a group of related classes and interfaces that are used to
organize and manage Java code.
● Packages are used to avoid naming conflicts, improve code reusability, and make
code easier to maintain.
● The Java Standard Library is a collection of packages that are included with the Java
Development Kit (JDK).
● These packages provide a wide range of functionality, including input/output
operations, networking, graphical user interfaces, and more.
● The Java library packages are organized in a hierarchical structure, with each package
containing sub-packages and classes.
● The top-level package is called "java", and it contains the core Java classes and
interfaces.
● Other top-level packages include "javax", which contains extensions to the Java
platform, and "org", which contains classes and interfaces for third-party libraries.
● Here are a few examples of Java library packages:
o [Link]: This package provides classes and interfaces for working with
collections, such as ArrayList, HashSet, and HashMap.
o [Link]: This package provides classes and interfaces for input/output
operations, such as reading and writing files, handling streams, and more.
o [Link]: This package provides classes and interfaces for networking
operations, such as creating sockets, sending and receiving data over the
network, and more.
o [Link]: This package provides classes and interfaces for creating graphical
user interfaces, such as windows, buttons, text fields, and more.
o [Link]: This package provides classes and interfaces for creating more
advanced graphical user interfaces, such as dialogs, menus, and toolbars.
● To use a class or interface from a package, you need to import the package into your
Java file using the "import" statement.

Example: The [Link] package


import [Link].*;

import [Link].*;

public class MyWindow extends JFrame {

public MyWindow() {

super("My Window");

1
setSize(400, 300);

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Hello, World!", [Link]);

[Link](new Font("Serif", [Link], 24));

getContentPane().add(label);

setVisible(true);

public static void main(String[] args) {

new MyWindow();

● In this example, we have imported the "[Link]." package, which provides classes
and interfaces for creating graphical user interfaces (GUIs).
● We have also imported the "[Link]." package, which provides classes and
interfaces for creating more advanced GUIs.
● We have defined a class called "MyWindow" that extends the JFrame class, which is a
top-level container for GUI components. I
● n the constructor of the "MyWindow" class, we have set the title of the window to
"My Window", set its size to 400x300 pixels, centered it on the screen, and set the
default close operation to exit the application when the window is closed.
● We have then created a JLabel component called "label" that displays the text
"Hello, World!" in the center of the window. We have set the font of the label to
"Serif" with a bold style and a size of 24 points. We have added the label to the
content pane of the JFrame using the "getContentPane()" method and the "add()"
method.
● Finally, we have made the window visible by calling the "setVisible()" method.

2
Example: the java util package
● Let's say you want to use the "[Link]" package, which contains useful classes for
working with collections, such as ArrayList and HashMap.
● First, you need to import the package at the top of your Java file:

import [Link].*;

● This statement imports all classes in the "[Link]" package, so you can use
them in your code.
● Next, you can create an ArrayList and add some elements to it like this:

ArrayList<String> myList = new ArrayList<String>();

[Link]("apple");

[Link]("banana");

[Link]("orange");

● Finally, you can use a for loop to iterate over the elements in the ArrayList and
print them out:

for(String fruit : myList) {

[Link](fruit);

You might also like